help with pic micro ADC - Page 1
Forum section: electronics / general

author: mj21
author_rank: Regular Contributor
timestamp: August 15, 2013, 11:19:59 pm
content: i am trying to display two sets of voltages on a lcd with PIC16f684. i can get one line with voltage displaying using ANSEL and selecting an line as an analogue input.but when i set more than one input as an analogue and set it to read, all i get is rubbish symbols on the LCD. is this even possible to do?any help will be appreciated.here is my code:// LCD module connectionssbit LCD_RS at RC4_bit;sbit LCD_EN at RC5_bit;sbit LCD_D4 at RC0_bit;sbit LCD_D5 at RC1_bit;sbit LCD_D6 at RC2_bit;sbit LCD_D7 at RC3_bit;sbit LCD_RS_Direction at TRISC4_bit;sbit LCD_EN_Direction at TRISC5_bit;sbit LCD_D4_Direction at TRISC0_bit;sbit LCD_D5_Direction at TRISC1_bit;sbit LCD_D6_Direction at TRISC2_bit;sbit LCD_D7_Direction at TRISC3_bit;// End LCD module connectionschar Message1[] = "Voltage:";unsigned int ADC_Value1, DisplayVolt1;char *volt = "00.00";char Message2[] = "Amperage:";unsigned int ADC_Value2, DisplayVolt2;char *volt1 = "00.00";void main() {ANSEL = 0b00010100;ADCON0 = 0b00010100;ADCON1 = 0x00;CMCON0 = 0x07 ;TRISC = 0b00000000;TRISA = 0b00010100;Lcd_Init();Lcd_Cmd(_LCD_CLEAR);Lcd_Cmd(_LCD_CURSOR_OFF);Lcd_Out(1,1,Message1);Lcd_Out(2,1,Message2);do {ADC_Value1 = ADC_Read(2);DisplayVolt1 = ADC_Value1 * 2;volt = DisplayVolt1/1000 + 48;volt = (DisplayVolt1/100)%10 + 48;volt = (DisplayVolt1/10)%10 + 48;volt = (DisplayVolt1/1)%10 + 48;Lcd_Out(1,9,volt);delay_ms(200);ADC_Value2 = ADC_Read(2);DisplayVolt2 = ADC_Value2 * 2;volt = DisplayVolt2/1000 + 48;volt = (DisplayVolt2/100)%10 + 48;volt = (DisplayVolt2/10)%10 + 48;volt = (DisplayVolt2/1)%10 + 48;Lcd_Out(2,10,volt);delay_ms(200);} while(1);}

author: Psi
author_rank: Super Contributor
timestamp: August 15, 2013, 11:41:21 pm
content: My pointer knowledge is a bit rusty but why are you using pointers here?char *volt = "00.00";char *volt1 = "00.00";EDIT:Nevermind, i see Lcd_out() uses a pointer.

author: mj21
author_rank: Regular Contributor
timestamp: August 15, 2013, 11:48:25 pm
content: this is what i got from another set of codes which seems to work for displaying voltage on LCD. it only stops working when i set two analogue channels as input.

author: c4757p
author_rank: Super Contributor
timestamp: August 16, 2013, 12:00:16 am
content: Changechar *volttochar volt[]. You can still pass it to Lcd_Out; it will be implicitly changed to a pointer.C string literals are immutable, you cannot change them.char *volt = "00.00";sets "volt" equal to the memory address of the immutable character sequence "00.00", it doesn't actually create an array in RAM that you can modify. C leaves off theconstfrom string literals for "historical reasons"but it's still there in practice.

author: mj21
author_rank: Regular Contributor
timestamp: August 16, 2013, 12:10:50 am
content: changed it to char volt1[];doing that results in error (Array dimension must be greater than 0).

author: Psi
author_rank: Super Contributor
timestamp: August 16, 2013, 12:18:55 am
content: Add the number of elements you want inside the []char volt = "00.00";char volt1 = "00.00";Note:, you're not actually using volt1 at all.

author: mj21
author_rank: Regular Contributor
timestamp: August 16, 2013, 12:24:02 am
content: same problem again.

author: mariush
author_rank: Super Contributor
timestamp: August 16, 2013, 12:36:40 am
content: And you're using ADC_Read(2) twice, you probably meant to use ADC_Read(1) for the first.See, this is a problem when you use a programming environment which hides all the stuff from you ... I guess this is Mikroe c or somethingWhere do you have adc_read defined? Where is LCD_Out defined? Where's the source code for those?Read the datasheet of the microcontroller and try to do the functions by yourself, you'll learn more.http://ww1.microchip.com/downloads/en/devicedoc/41202f-print.pdf

author: mj21
author_rank: Regular Contributor
timestamp: August 16, 2013, 12:45:10 am
content: tnx guys for the help, i managed to fix the damn thing. i seems the analogue pin i was assigning was also used for the oscillator so it conflicted as i did not set it to use internal oscillator.thanks again for your quick responses.