Author Topic: help with pic micro ADC  (Read 3046 times)

0 Members and 1 Guest are viewing this topic.

Offline mj21

  • Regular Contributor
  • *
  • Posts: 59
  • Country: gb
help with pic micro ADC
« on: August 15, 2013, 11:19:59 pm »
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 connections
sbit 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 connections

char 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[0] = DisplayVolt1/1000 + 48;
   volt[1] = (DisplayVolt1/100)%10 + 48;
   volt[3] = (DisplayVolt1/10)%10 + 48;
   volt[4] = (DisplayVolt1/1)%10 + 48;
   Lcd_Out(1,9,volt);
   delay_ms(200);


    ADC_Value2 = ADC_Read(2);
    DisplayVolt2 = ADC_Value2 * 2;
    volt[0] = DisplayVolt2/1000 + 48;
    volt[1] = (DisplayVolt2/100)%10 + 48;
    volt[3] = (DisplayVolt2/10)%10 + 48;
    volt[4] = (DisplayVolt2/1)%10 + 48;
    Lcd_Out(2,10,volt);
    delay_ms(200);
  } while(1);
 }
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7592
  • Country: nz
Re: help with pic micro ADC
« Reply #1 on: August 15, 2013, 11:41:21 pm »
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.
« Last Edit: August 15, 2013, 11:50:12 pm by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline mj21

  • Regular Contributor
  • *
  • Posts: 59
  • Country: gb
Re: help with pic micro ADC
« Reply #2 on: August 15, 2013, 11:48:25 pm »
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.

 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7805
  • Country: us
  • adieu
Re: help with pic micro ADC
« Reply #3 on: August 16, 2013, 12:00:16 am »
Change char *volt to char 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 the const from string literals for "historical reasons"  ::) but it's still there in practice.
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline mj21

  • Regular Contributor
  • *
  • Posts: 59
  • Country: gb
Re: help with pic micro ADC
« Reply #4 on: August 16, 2013, 12:10:50 am »
changed it to char volt1[];
doing that results in error (Array dimension must be greater than 0).
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7592
  • Country: nz
Re: help with pic micro ADC
« Reply #5 on: August 16, 2013, 12:18:55 am »
Add the number of elements you want inside the []

char volt[5] = "00.00";
char volt1[5] = "00.00";


Note:, you're not actually using volt1 at all.
« Last Edit: August 16, 2013, 12:21:21 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline mj21

  • Regular Contributor
  • *
  • Posts: 59
  • Country: gb
Re: help with pic micro ADC
« Reply #6 on: August 16, 2013, 12:24:02 am »
same problem again.
 

Offline mariush

  • Super Contributor
  • ***
  • Posts: 3954
  • Country: ro
  • .
Re: help with pic micro ADC
« Reply #7 on: August 16, 2013, 12:36:40 am »
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 something

Where 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
 

Offline mj21

  • Regular Contributor
  • *
  • Posts: 59
  • Country: gb
Re: help with pic micro ADC
« Reply #8 on: August 16, 2013, 12:45:10 am »
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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf