nakatatran
Trứng gà
Đây là code đếm sản phẩm hiện thị lên LCD + giao tiếp PC
nhưng phần giao tiếp PC mình chưa làm được ...mong các bạn giúp mình với.Cám ơn!
HTML:
#include <htc.h>
#include <stdio.h> //thu vien dung cho printf
#include "lcd.h" //thu vien dung cho LCD
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & BORDIS & IESODIS & FCMDIS & LVPDIS);
unsigned int tc;
unsigned char vang,doo,xanh,tam,tamx,tamd,tamv,cosanpham;
void main(void)
{
ANSEL = ANSELH = 0;
WPUB = 0x00; //KEO LEN TREN CHAN RB0
TRISD = 0X00;
//2.Khoi tao LCD
lcd_init();
TRISB = 0X00;
PORTB=0xf0;
xanh=vang=doo=0;
printf("\fXANH:%d-VANG:%d-DO:%d\n-TC:%d",vang,xanh,doo,tc);
while(1)
{
TRISB = 0XFF;
tam =PORTB;
switch(tam)
{
case 0xdF://RB5
tamx=1;
cosanpham=1;
break;
case 0xbF:
tamd=1;
cosanpham=1;
break;
case 0x7F:
tamv=1;
cosanpham=1;
break;
default:
cosanpham=0;
break;
}
tam =PORTB;
if(tam==0xFF) // cho nha phim => P1=f0
{
if(tamx==1)
xanh++;
tamx=0;
if(tamd==1)
doo++;
tamd=0;
if(tamv==1)
vang++;
tamv=0;
}
tc=xanh+vang+doo;
if (cosanpham==1)
{
printf("\fXANH:%d-VANG:%d-\nDO:%d----TC:%d",vang,xanh,doo,tc);
cosanpham=0;
}
}
}
void putch(char c)
{
lcd_putc(c);
}
HTML:
#include <htc.h>
#include <stdio.h> //thu vien dung cho printf
#include "lcd.h" //thu vien dung cho LCD
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & BORDIS & IESODIS & FCMDIS & LVPDIS);
unsigned int tc;
unsigned char vang,doo,xanh,tam,tamx,tamd,tamv,cosanpham;
void main(void)
{
//Chan RC6 la chan TX - ouput
TRISC6 = 0;
//Chan RC7 la chan RX - input
TRISC7 = 1;
//cau hinh RB0 la chan input
//TRISB0 = 1;
//Weakpullup RB0
//WPUB0 = 1;
//RBPU = 0;
//Khoi tao UART, che do bat dong bo, data 8bit, 1start, 1stop, noparity, 19200baud
//TXSTA
//data 8-bit
TX9 = 0;
//Cho phep truyen UART
TXEN = 1;
//Che do bat dong bo
SYNC = 0;
//BRGH //doi cau hinh Baud roi tinh tiep
//RCSTA
//Cho phep serial port hoat dong
SPEN = 1;
//Receive 8-bit
RX9 = 0;
CREN = 1;
//Toc do baud 19200
BRGH = 1;
BRG16 = 1;
SPBRGH = 0;
SPBRG = 51;
// PORTD = 0xC0;
//-----------------------------------
RCIE=1; //CHO PHEP NGAT NHAN
PEIE=1; //
GIE=1; //+ Cho phép ng?t toàn c?c - GIE = 1
//-----------------------------------------------
ANSEL = ANSELH = 0;
WPUB = 0x00; //KEO LEN TREN CHAN RB0
TRISD = 0X00;
//2.Khoi tao LCD
lcd_init();
TRISB = 0X00;
PORTB=0xf0;
xanh=vang=doo=0;
printf("\fXANH:%d-VANG:%d-DO:%d\n-TC:%d",vang,xanh,doo,tc);
while(1)
{
TRISB = 0XFF;
tam =PORTB;
switch(tam)
{
case 0xdF://RB5
tamx=1;
cosanpham=1;
break;
case 0xbF:
tamd=1;
cosanpham=1;
break;
case 0x7F:
tamv=1;
cosanpham=1;
break;
default:
cosanpham=0;
break;
}
tam =PORTB;
if(tam==0xFF) // cho nha phim => P1=f0
{
if(tamx==1)
xanh++;
tamx=0;
putchar(xanh);
if(tamd==1)
doo++;
tamd=0;
putchar(doo);
if(tamv==1)
vang++;
tamv=0;
putchar(vang);
}
tc=xanh+vang+doo;
putchar(tc);
if (cosanpham==1)
{
printf("\fXANH:%d-VANG:%d-\nDO:%d----TC:%d",vang,xanh,doo,tc);
cosanpham=0;
}
}
}
[COLOR="red"]//void putch(char c)
//{
// lcd_putc(c);
//}[/COLOR]
//-----------------------------------
//Goi 1 ky tu a ra UART
void putchar(char c)
{
TXREG = c;
while(!TXIF);
TXIF = 0;
}
//-----------------------------------
void interrupt uart_getc(){
while(!RCIF);
RCIF=0;
PORTD=RCREG;
}
void delay_ms (int ms)
{
int i;
for (i=0;i<ms;i++){
_delay(1000);
}
}