Trích:
Nguyên văn bởi ElecTel
các anh cho em hỏi đoạn code em viết hiển thị cho LCD như này đã đúng chưa?
em muốn tạo ra 1 phím nhấn tăng count với 1 phím nhấn giảm count sau đó hiển thị count lên lcd
Code:
Void push_key()
{
int count;
if(B1==0) //nut o chan B1 duoc nhan
{
delay_ms(150); //delay qua xung nhieu
while(B1==1) //cho den khi tha
{;}
count++; //dem so lan bam nut
}
else if(B2==0) //nut o chan b2 duoc nhan
{
delay_ms(150);
while(B2==1)
{;}
count--; //dem so lan bam nut
}
if(count<0)
count=0;
else if(count>99)
count=99
return count;
}
void menu_control()
{
lcd_init();
lcd_gotoxy(9,1); // cot 9 hang 1
lcd_putc("Menu controler");
lcd_gotoxy(1,2);
lcd_putc("K=");
lcd_gotoxy(3,2);
lcd_putc(count); // cho này em viết vậy nó có hiển thị số count đếm được ko ?
}
|
tôi gửi cho bạn đoạn code hoàn chỉnh để bạn tham khảo.
Code:
#include <16F877A.h>
#device *=16 adc=10
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=4000000)
#include <lcd.c>
#define LCD_DATA_PORT getenv("SFR:PORTD")
#define LCD_ENABLE_PIN PIN_D0 ////
#define LCD_RS_PIN PIN_D1 ////
#define LCD_RW_PIN PIN_D2
#define LCD_DATA4 PIN_D4 ////
#define LCD_DATA5 PIN_D5 ////
#define LCD_DATA6 PIN_D6 ////
#define LCD_DATA7 PIN_D7 ////
#byte portB =0x06 ////
#bit B1= portB.1 ////
#bit B2= portB.2 ////
void push_key(void);
main()
{
set_tris_b(0b00000110);
port_b_pullups(true);
set_tris_c(0b00000000);
lcd_init();
lcd_gotoxy(1,1); // cot 1 hang 1
lcd_putc("Menu controler");
lcd_gotoxy(1,2);
lcd_putc("K=");
while(1)
{
push_key();
}
}
void push_key()
{
int count;
if(B1==0) //nut o chan B1 duoc nhan
{
delay_ms(10); //delay qua xung nhieu
while(B1==0) //cho den khi tha
{;}
count++; //dem so lan bam nut
lcd_gotoxy(3,2);
lcd_putc(" ");
lcd_gotoxy(3,2);
Printf(LCD_putc,"%u",count); // hiển thị số count đếm lên LCD
delay_ms(10); //delay qua xung nhieu
}
else if(B2==0) //nut o chan b2 duoc nhan
{
delay_ms(10);
while(B2==0)
{;}
count--; //dem so lan bam nut
lcd_gotoxy(3,2);
lcd_putc(" ");//
lcd_gotoxy(3,2);
Printf(LCD_putc,"%u",count); // hiển thị số count đếm lên LCD
delay_ms(10); //delay qua xung nhieu
}
else
{
}
}