//Crystal Frequency: 20MHz
//Compiler: HI-TECH ANSI C
#include
#include
__CONFIG(0x3F32);
#define LCD_RS RD0
#define LCD_RW RD1
#define LCD_EN RD2
#define LCD_LIGHT RD3
#define LCD_DATA PORTD //D7-D4
#define LCD_PULSE() ((LCD_EN=1),(LCD_EN=0))
#define BAUD 9600
#define _XTAL_FREQ 20000000
void pic_init(void);
void uart_init(void);
void lcd_init(void);
void lcd_write(unsigned char c);
void lcd_clear(void);
void lcd_goto(unsigned char pos);
void lcd_string(const char *s);
void convert(unsigned int no, char base);
void display(char number);
void uart_transmit(char data);
void uart_string(const char *s);
char mode,cam[40],di[10],z,rec[]={"ACK"};
unsigned int counter[2];
static void interrupt isr(void)
{char i;
if(RCIF==1){
counter[0]=0;
if(RCREG=='T') z=0;
cam[z]=RCREG;
if(z<38) z++;} if(TMR0IF==1){ TMR0IF=0; if(counter[0]<20000) counter[0]++; if(counter[0]==5000) z=0; } } main() {int i,j,space,value[8]; pic_init(); //initialize PIC uart_init(); //initialize UART lcd_init(); //initialize LCD lcd_goto(0x00); //select first line lcd_string(" CMUCAM EXAMPLE "); //display string lcd_goto(0x40); //select second line lcd_string(" "); //display string lcd_goto(0x14); //select third line lcd_string(" "); //display string lcd_goto(0x54); //select fourth line lcd_string(" "); //display string LCD_RS=0; lcd_write(0x40); LCD_RS=1; for(i=0;i<=63;i++) lcd_write(0b00000); mode=0; counter[1]=0; for(;;){ counter[1]++; switch(mode){ case 0:{ if(counter[1]==1){ for(i=0;i<=39;i++) cam[i]=0x20; lcd_goto(0x40); //select second line lcd_string("RS "); //display string uart_string("RS"); uart_transmit(0x0D);} //Enter else if(counter[1]==1000){ counter[1]=0; j=0; for(i=0;i<=2;i++){ if(cam[i]==rec[i]) j++;} if(j==3){ counter[1]=0; mode++;}} lcd_goto(0x14); //select third line LCD_RS=1; for(i=0;i<=19;i++) lcd_write(cam[i]); lcd_goto(0x54); //select fourth line LCD_RS=1; for(i=20;i<=39;i++) lcd_write(cam[i]); break;} case 1:{ if(counter[1]==1){ for(i=0;i<=39;i++) cam[i]=0x20; lcd_goto(0x40); //select second line lcd_string("CR 18 44 "); //display string uart_string("CR"); uart_transmit(0x20); //Space uart_string("18"); uart_transmit(0x20); //Space uart_string("44"); uart_transmit(0x0D);} //Enter else if(counter[1]==1000){ counter[1]=0; j=0; for(i=0;i<=2;i++){ if(cam[i]==rec[i]) j++;} if(j==3){ counter[1]=0; mode++;}} lcd_goto(0x14); //select third line LCD_RS=1; for(i=0;i<=19;i++) lcd_write(cam[i]); lcd_goto(0x54); //select fourth line LCD_RS=1; for(i=20;i<=39;i++) lcd_write(cam[i]); break;} case 2:{ if(counter[1]==1){ for(i=0;i<=39;i++) cam[i]=0x20; lcd_goto(0x40); //select second line lcd_string("TC 0 40 0 40 0 40 "); //display string uart_string("TC"); uart_transmit(0x20); //Space uart_string("0"); uart_transmit(0x20); //Space uart_string("40"); uart_transmit(0x20); //Space uart_string("0"); uart_transmit(0x20); //Space uart_string("40"); uart_transmit(0x20); //Space uart_string("0"); uart_transmit(0x20); //Space uart_string("40"); uart_transmit(0x0D);} //Enter for(i=0;i<=7;i++) value[i]=0; space=0; i=2; do{ if((cam[i]<0x30)|(cam[i]>0x39)) space++;
else value[space]=10*value[space]+cam[i]-0x30;
i++;}
while(space!=8);
lcd_goto(0x14);
lcd_string(" ");
convert(value[0],10);
for(i=2;i>=0;i--) display(di[i]);
lcd_string(" ");
convert(value[1],10);
for(i=2;i>=0;i--) display(di[i]);
lcd_string(" ");
convert(value[2],10);
for(i=2;i>=0;i--) display(di[i]);
lcd_string(" ");
convert(value[3],10);
for(i=2;i>=0;i--) display(di[i]);
lcd_string(" ");
lcd_goto(0x54);
lcd_string(" ");
convert(value[4],10);
for(i=2;i>=0;i--) display(di[i]);
lcd_string(" ");
convert(value[5],10);
for(i=2;i>=0;i--) display(di[i]);
lcd_string(" ");
convert(value[6],10);
for(i=2;i>=0;i--) display(di[i]);
lcd_string(" ");
convert(value[7],10);
for(i=2;i>=0;i--) display(di[i]);
lcd_string(" ");
break;}
default:{
mode=0;
break;}}
}}
void pic_init(void)
{
TRISA=0b00000000;
TRISB=0b00000000;
TRISC=0b10000000;
TRISD=0b00000000;
TRISE=0b00000000;
OPTION=0b00000000;
ADCON1=0b00000110;
INTCON=0b11100000;
PIE1=0b00100000; //Enable RX interrupt
PORTA=0b00000000;
PORTB=0b00000000;
PORTC=0b00000000;
PORTD=0b00000000;
PORTE=0b00000000;
}
void uart_init(void)
{
TXSTA=0b10100100;
RCSTA=0b10010000;
SPBRG=(int)(_XTAL_FREQ/(16.0*BAUD)-1);
}
/* initialise the LCD - put into 4 bit mode */
void lcd_init(void)
{
__delay_ms(15); //delay for LCD Power Up
lcd_write(0x28); //function set
lcd_write(0x0C); //display on/off control
lcd_clear(); //clear screen
lcd_write(0x06); //entry mode set
LCD_LIGHT=1;
}
/* write a byte to the LCD in 4 bit mode */
void lcd_write(unsigned char c)
{
LCD_DATA=(LCD_DATA&0x0F)|(c&0xF0);
LCD_PULSE();
LCD_DATA=(LCD_DATA&0x0F)|((c<<4)&0xF0);
LCD_PULSE();
__delay_us(40);
}
/* clear LCD and goto home */
void lcd_clear(void)
{
LCD_RS=0;
lcd_write(0x1);
__delay_ms(2);
}
/* write a string of chars to the LCD */
void lcd_string(const char *s)
{
LCD_RS=1;
while(*s)
lcd_write(*s++);
}
/* go to the specified position */
void lcd_goto(unsigned char pos)
{
LCD_RS=0;
lcd_write(0x80+pos);
}
void convert(unsigned int no, char base)
{char i;
for(i=0;i<=9;i++) di[i]=0;
i=0;
do{
di[i]=no%base;
no=no/base;
i=i+1;}
while(no!=0);
}
void display(char number)
{switch(number){
case 0:{lcd_string("0");break;}
case 1:{lcd_string("1");break;}
case 2:{lcd_string("2");break;}
case 3:{lcd_string("3");break;}
case 4:{lcd_string("4");break;}
case 5:{lcd_string("5");break;}
case 6:{lcd_string("6");break;}
case 7:{lcd_string("7");break;}
case 8:{lcd_string("8");break;}
case 9:{lcd_string("9");break;}
case 10:{lcd_string("A");break;}
case 11:{lcd_string("B");break;}
case 12:{lcd_string("C");break;}
case 13:{lcd_string("D");break;}
case 14:{lcd_string("E");break;}
case 15:{lcd_string("F");break;}
default:{lcd_string("?");break;}
}}
void uart_transmit(char data){
while(TXIF==0) continue;
TXREG=data;
}
void uart_string(const char *s)
{
while(*s)
uart_transmit(*s++);
}
No comments:
Post a Comment