Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <htc.h>
- #define data PORTB
- #define RS RD0
- #define EN RD1
- __CONFIG(HS & WDTDIS & PWRTEN & BORDIS & LVPDIS);
- float valch0,valch1;
- void delay_ms(int n )
- {
- TMR1H=0xEC;
- TMR1L=0x77;
- T1CKPS1=0;
- T1CKPS0=0;
- TMR1CS=0;
- TMR1IF=0;
- TMR1ON=1;
- while(n>0)
- {
- while(!TMR1IF);
- TMR1IF=0;
- TMR1H=0xEC;
- TMR1L=0x77;
- n--;
- }
- }
- float select_adc(unsigned char chnl){ // init_adc(channel name);
- CHS2=CHS1=0;CHS0=chnl;
- delay_ms(10);
- ADON=1;
- ADGO=1;
- while(ADGO);
- return (((ADRESH*256+ADRESL)*5.0/1023)*10);
- }
- void LCD_Write(unsigned char values,int rs){
- data = values;
- RS = rs; // rs=0 command and rs=1 data
- EN = 1;
- delay_ms(1);
- EN = 0;
- }
- void LCD_clear(void){
- LCD_Write(0x01,0); //this clears LCD
- }
- void LCD_goto(unsigned char row,unsigned char column){
- if(row==1){
- LCD_Write(0x80+column,0);
- }
- else if(row==2){
- LCD_Write(0xC0+column,0);
- }
- }
- void LCD_num(int n){
- LCD_Write((n/100)+48,1);
- LCD_Write(((n%100)/10)+48,1);
- LCD_Write((n%10)+48,1);
- }
- void initLCD(void){
- TRISD=0x00;//as output
- TRISB = 0x00;
- delay_ms(100);
- LCD_Write(0x30, 0); //8 - bit display
- EN=1; EN = 0;
- LCD_Write(0x38,0); //2 lines mode
- LCD_Write(0x0C,0); //dispaly on , cursor blinking
- delay_ms(1);
- LCD_clear();LCD_goto(1,0);delay_ms(1);
- LCD_goto(1,7);
- LCD_Write('D',1);delay_ms(100);
- LCD_Write('V',1);delay_ms(100);
- LCD_Write('M',1);delay_ms(100);
- LCD_goto(2,12);
- LCD_Write('v',1);delay_ms(100);
- LCD_goto(2,7);
- LCD_Write('.',1);delay_ms(100);
- }
- void main(void){
- PCFG3=PCFG2=PCFG1=PCFG0=0;
- ADFM=1;
- ADCS1=1;
- ADCS0=0;
- TRISA=0xFF;
- initLCD();
- while(1){
- valch0=select_adc(0);
- valch1=select_adc(1);
- if(valch0 <= 0.00 && valch1!=0){
- valch0=valch1;
- LCD_goto(2,1);
- LCD_Write('-',1);
- delay_ms(5);
- }
- else{
- LCD_goto(2,1);
- LCD_Write('+',1);
- delay_ms(5);
- }
- LCD_goto(2,4);
- LCD_num(valch0);
- LCD_goto(2,8);
- LCD_num((valch0-(int)valch0)*1000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement