flamesier
Newbie level 6
- Joined
- Oct 2, 2010
- Messages
- 13
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,453
// Define LCD
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB3_bit;
sbit LCD_D5 at RB2_bit;
sbit LCD_D6 at RB1_bit;
sbit LCD_D7 at RB0_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB3_bit;
sbit LCD_D5_Direction at TRISB2_bit;
sbit LCD_D6_Direction at TRISB1_bit;
sbit LCD_D7_Direction at TRISB0_bit;
unsigned int adcvalue,value;
unsigned char car,x,y;
char *voltage = "00.00";
long temp;
void ShowADC(int x, int y, unsigned int adcvalue) // Routine to show the value of the ADC_read
{
car = adcvalue / 1000;
LCD_Chr(x,y,48+car);
adcvalue=adcvalue-1000*car;
car = (adcvalue / 100);
LCD_Chr_CP(48+car);
adcvalue=adcvalue-100*car;
car = (adcvalue / 10);
LCD_Chr_CP(48+car);
adcvalue=adcvalue-10*car;
car = adcvalue;
LCD_Chr_CP(48+car);
delay_ms(30);
}
void ShowVoltage (int x,int y, unsigned int value) // Routine to show the ADC_read conversion in Volt
{
temp = (long)value* 5000;
temp = temp / 1023;
voltage[0] = temp/10000 + 48;
voltage[1] = (temp/1000)%10 + 48;
voltage[3] = (temp/100)%10 + 48;
voltage[4] = (temp/10)%10 + 48;
Lcd_Out (x,y,voltage);
delay_ms(30);
}
void main() {
ADCON1 = 0x80; // All analogs - right justify
TRISA = 0xFF; // PORTA Input
TRISB = 0; // PORTB Output
TRISD = 0;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1, 1, " ADC Voltage");
do {
value = ADC_Read(1); // channel 1 (RA1)
adcvalue=value;
Lcd_Out(3,1,"Ch1:");
ShowADC (3,5,adcvalue);
ShowVoltage(3,11,value);
value = ADC_Read(2); // channel 2 (RA2)
adcvalue=value;
Lcd_Out(4,1,"Ch2:");
ShowADC (4,5,adcvalue);
ShowVoltage(4,11,value);
} while(1);
}
temp = (long)value* 5000;
temp = temp / 1023;
If your adc value is 1023 for 4.99V then
1023 * x = 4.99V
x = 4.99V/1023
x = 0.0048778103616813
1/x = 205.0100200400802
adc_val = adc_val * 0.0048778103616813
or
adc_val = adc_val / 205.0100200400802
Another way to calculate x is
adc_val = adc_val * Vref / 1023
x = Vref / 1023
x = 5 / 1023
x = 0.0048875855327468
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 void main(){ unsigned char str_adc_val[17]; unsigned int i = 0; while(1)[ adc_val = adc_val * 5 / 1023; FloatToStr(adc_val, str_adc_val); while(str_adc_val[i]){ UART1_Write(str_adc_val[i++]); } } }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 //lcd module connections sbit LCD_RS at RB0_bit; sbit LCD_EN at RB1_bit; sbit LCD_D4 at RB4_bit; sbit LCD_D5 at RB5_bit; sbit LCD_D6 at RB6_bit; sbit LCD_D7 at RB7_bit; sbit LCD_RS_Direction at TRISB0_bit; sbit LCD_EN_Direction at TRISB1_bit; sbit LCD_D4_Direction at TRISB4_bit; sbit LCD_D5_Direction at TRISB5_bit; sbit LCD_D6_Direction at TRISB6_bit; sbit LCD_D7_Direction at TRISB7_bit; void main() { float adc_val = 0.0, old_val = 0.0; unsigned char str_adc_val[17]; unsigned int i = 0; TRISA = 0b00000100; PORTA = 0x00; TRISB = 0x00; PORTB = 0x00; TRISC = 0x80; PORTC = 0x00; ANSEL = 0b00000100; ANSELH = 0x00; ADCON1 = 0x80; CM1CON0 = 0x00; CM2CON0 = 0x00; VRCON.VROE = 0; LCD_Init(); LCD_Cmd(_LCD_CLEAR); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Out(1,1,"ADC XBee Example"); UART1_Init(9600); Delay_ms(100); while(1){ adc_val = ADC_Read(2); adc_val = adc_val * 5 / 1023; if(adc_val != old_val){ old_val = adc_val; FloatToStr(adc_val, str_adc_val); LCD_Out(2,1,str_adc_val); i = 0; while(str_adc_val[i]){ UART1_Write(str_adc_val[i++]); } UART1_Write(0x0D); UART1_Write(0x0A); } } }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 //lcd module connections sbit LCD_RS at RB0_bit; sbit LCD_EN at RB1_bit; sbit LCD_D4 at RB4_bit; sbit LCD_D5 at RB5_bit; sbit LCD_D6 at RB6_bit; sbit LCD_D7 at RB7_bit; sbit LCD_RS_Direction at TRISB0_bit; sbit LCD_EN_Direction at TRISB1_bit; sbit LCD_D4_Direction at TRISB4_bit; sbit LCD_D5_Direction at TRISB5_bit; sbit LCD_D6_Direction at TRISB6_bit; sbit LCD_D7_Direction at TRISB7_bit; void main() { float adc_val = 0.0, old_val = 0.0; unsigned char str_adc_val[17]; unsigned int i = 0; TRISA = 0b00000100; PORTA = 0x00; TRISB = 0x00; PORTB = 0x00; TRISC = 0x80; PORTC = 0x00; ANSEL = 0b00000100; ANSELH = 0x00; ADCON1 = 0x80; CM1CON0 = 0x00; CM2CON0 = 0x00; VRCON.VROE = 0; LCD_Init(); LCD_Cmd(_LCD_CLEAR); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Out(1,1,"ADC XBee Example"); UART1_Init(9600); Delay_ms(100); while(1){ adc_val = ADC_Read(2); adc_val = adc_val * 5 / 1023; if(adc_val != old_val){ old_val = adc_val; FloatToStr(adc_val, str_adc_val); LCD_Out(2,1,str_adc_val); i = 0; //while(str_adc_val[i]){ //UART1_Write(str_adc_val[i++]); //} UART1_Write_Text(str_adc_val); UART1_Write(0x0D); UART1_Write(0x0A); } } }
Code C - [expand] 1 adc_val = adc_val * 5 / 1023;
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 //lcd module connections sbit LCD_RS at RB0_bit; sbit LCD_EN at RB1_bit; sbit LCD_D4 at RB4_bit; sbit LCD_D5 at RB5_bit; sbit LCD_D6 at RB6_bit; sbit LCD_D7 at RB7_bit; sbit LCD_RS_Direction at TRISB0_bit; sbit LCD_EN_Direction at TRISB1_bit; sbit LCD_D4_Direction at TRISB4_bit; sbit LCD_D5_Direction at TRISB5_bit; sbit LCD_D6_Direction at TRISB6_bit; sbit LCD_D7_Direction at TRISB7_bit; void main() { float adc_val = 0.0, old_val = 0.0; unsigned char str_adc_val[17]; //unsigned int i = 0; TRISA = 0b00000100; PORTA = 0x00; TRISB = 0x00; PORTB = 0x00; TRISC = 0x80; PORTC = 0x00; ANSEL = 0b00000100; ANSELH = 0x00; ADCON1 = 0x80; CM1CON0 = 0x00; CM2CON0 = 0x00; VRCON.VROE = 0; LCD_Init(); LCD_Cmd(_LCD_CLEAR); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Out(1,1,"ADC XBee Example"); UART1_Init(9600); Delay_ms(100); while(1){ adc_val = ADC_Read(2); if(adc_val != old_val){ old_val = adc_val; adc_val = adc_val * 5 / 1023; FloatToStr(adc_val, str_adc_val); LCD_Out(2,1,str_adc_val); //i = 0; //while(str_adc_val[i]){ //UART1_Write(str_adc_val[i++]); //} UART1_Write_Text(str_adc_val); UART1_Write(0x0D); UART1_Write(0x0A); } } }
adc_val = ADC_Read(2);
adc_val = (adc_val * 5 / 1023)*60 ;
if(adc_val != old_val){
old_val = adc_val;
FloatToStr(adc_val, str_adc_val);
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?