Author Topic: ADC input -> blinking LED  (Read 2856 times)

0 Members and 1 Guest are viewing this topic.

Offline Yr0gErGTopic starter

  • Jr. Member
  • **
  • Posts: 18
  • Helpful? 0
ADC input -> blinking LED
« on: September 24, 2010, 12:11:51 AM »
I'm trying to get a LED to blink when a sensor input reaches a high enough value using ADC
here's my code:
#include <p18f4520.h>
#include <delays.h>

//////CONFIG BITS////////////////

#pragma config WDT = OFF   //watchdog timer
#pragma config OSC = HS      //oscillator set to high speed
#pragma config LVP = OFF   //low voltage program off
#pragma config MCLRE = ON   //mclr pin active (frees up RE3)

//////////////////////////////////

void main(){
   TRISD = 0b00000000; //set all pins on port D to be output
   PORTD = 0b00000000; //set all pins on port D to be low (0v)
   ADCON0 = 0b00000001;//READ ANALOG VOLTAGE FROM RA0
   ADCON1 = 0b00001110;//CONFIGURE ADC, RA0
   ADCON2 = 0b00000001;
   TRISA = 0b00000001;// RA0 INPUT   
   int t;


    while(1){
      Delay10TCYx(12);
        ADCON0bits.GO = 1;
        while(ADCON0bits.DONE);
        t=ADRESH*256+ADRESL;

        if(t>255){
         int x=t;
            while(1){
               if(   PORTDbits.RD0 == 0){   //if the pin is off
                  PORTDbits.RD0 = 1;   //turn it on (send 5v out of the pin)
                                 }
               else{
                  PORTDbits.RD0 = 0;   //otherwise it must already be
                                 //on so turn it off (set the pin to 0v)
                  }

               Delay10KTCYx(200);   //set a delay (adjusting the value in ms)
                              //in brackets adjusts the speed
                              //of the port turning on and off)

               }
              }
          }
      
}      

but i keep getting a syntax error on the line int t;
is there something i've overlooked?

Offline Yr0gErGTopic starter

  • Jr. Member
  • **
  • Posts: 18
  • Helpful? 0
Re: ADC input -> blinking LED
« Reply #1 on: September 24, 2010, 01:25:06 AM »
figured out the n00b reason for the syntax error, but the problem is that my LED is always blinking. does anyone know why that might be?

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: ADC input -> blinking LED
« Reply #2 on: September 24, 2010, 07:38:05 AM »
Move the int t; to above any code that assigns values, that is above the TRISD =  .

Code: [Select]
            while(1){
               if(   PORTDbits.RD0 == 0){   //if the pin is off
                  PORTDbits.RD0 = 1;   //turn it on (send 5v out of the pin)
                                 }
               else{
                  PORTDbits.RD0 = 0;   //otherwise it must already be
                                 //on so turn it off (set the pin to 0v)
                  }

               Delay10KTCYx(200);   //set a delay (adjusting the value in ms)
                              //in brackets adjusts the speed
                              //of the port turning on and off)
              }
Execution NEVER gets out of this while(1) loop and back to the if(t>255) conditional test.

 


Get Your Ad Here

data_list