PIC16f690, initialisation of ports, LEDS,reading from a pin

Discussion in 'Embedded Systems and Microcontrollers' started by fantabulous68, Aug 24, 2009.

  1. fantabulous68

    Thread Starter Active Member

    Nov 3, 2007
    51
    4
    PIC16f690, initialisation of ports, LEDS,reading from a pin

    I want an LED connected to pin RAO to glow if pin RA1==0

    when i run my code the LED keeps pulsing on and off regardless of what value RA1 equals to.

    could someone plz help me. Thanks

    Code ( (Unknown Language)):
    1.  
    2. #include <pic.h>
    3. #include "pic.h"
    4. #include "delay.h"
    5. #include "math.h"
    6. #include <stdio.h>
    7. #include <stdlib.h>
    8.  
    9. void init(void)
    10. {  
    11.     TRISB=0x00;
    12.     TRISC=0xFC;
    13.     ANSEL=0;        // Set inputs to digital
    14.        TRISA=0x02;  //making pin RA1 input
    15. }
    16.  
    17. void main(void)
    18. {
    19.     init(); // initialise I/O ports, LCD
    20.     while(1)                        //  Loop forever
    21.   {
    22.  
    23.     if(RA1==0)                  //if receiver detects something then output is active low
    24.             {PORTA=0x01;}   //light up led connected to RA0 if detect object
    25.     else
    26.             {PORTA=0x00;}   //dont light led if no detection
    27.   }
    28. }
    29.  
     
  2. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    1,015
    69
    I can't see anything obviously wrong, but I am half asleep, sorry.

    Do you have the chip config set correctly for the oscillator type, and is the watchdog disabled?

    You should be able to turn on the register view and run it in MPLab's built-in simulator to see what is happening step-by-step.
     
  3. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    If there is a reset pin you have to pull it up with a 10K resistor.
     
  4. Dragonblight

    Active Member

    Aug 19, 2009
    35
    0
    You know what, I know this is off topic as a (profanity), but what the (profanity) is a pull up resistor?
     
  5. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    It is off topic and also hijacking the thread.

    Ask the question in a new thread and you might get an answer.
     
  6. Dragonblight

    Active Member

    Aug 19, 2009
    35
    0
    If we're going to niggle on forum etiquette details, so be it.
     
  7. fantabulous68

    Thread Starter Active Member

    Nov 3, 2007
    51
    4
    After I disabled the watchdog timer, it fixed an LCD displaying error that i was getting. Thank you so much. I didnt know that i supposed to disable it. I am a beginner. im using a PIC 16f690.

    what do u mean. This is all the code i have written relating to the oscillator. What bits do i need to set???

    Code ( (Unknown Language)):
    1.  
    2. /////////////////////////////DELAY///////////////////////////////
    3. void DelayMs(unsigned char cnt)
    4. {
    5. #if XTAL_FREQ <= 2MHZ
    6.     do {
    7.         DelayUs(996);
    8.     } while(--cnt);
    9. #endif
    10.  
    11. #if    XTAL_FREQ > 2MHZ
    12.     unsigned char   p;
    13.     do {
    14.         p = 4;
    15.         do {
    16.             DelayUs(250);
    17.         } while(--p);
    18.     } while(--cnt);
    19. #endif
    20. }
    21.  
    22. void DelayS(unsigned char cnt)
    23. {
    24.     for (j=0; j<(cnt*10); j++)
    25.         DelayMs(100);
    26. }
    27. ///////////////////////////DELAY END/////////////////////////////
    28.  
    29. void init(void)
    30. {
    31.     OSCCON|=0x60; //set fosc to 4Mhz
    32.     TRISB=0x00;
    33.     TRISC=0xFC;
    34.     ANSEL=0;    //
    35.     TRISA=0x02; //
    36.     //ANSELH=0x00;
    37.     lcd_init(); //call LCD initialisation
    38.     GIE=0; //disable global interrupt
    39. }
    40.  
    41.  
    it used to display infrared liquid level detecto
    now it displays infrared liquid level detector.
     
  8. fantabulous68

    Thread Starter Active Member

    Nov 3, 2007
    51
    4
    i have an infrared receiver module which outputs a low when it detects an object. The receiver is is connected to RA1. Ill be using switches later in my design, thanks for telling me about the pull ups.
     
  9. fantabulous68

    Thread Starter Active Member

    Nov 3, 2007
    51
    4
    My design is an infrared liquid level detector. Im in the beginning stages of my design. I want to get the led indicator working for now. The hardware for my proximity detection circuit is working. When an object is detected the receiver IC outputs a low which is fed INTO pin RA1. i want an LED connected to RA0 to glow when detection occurs. The LCD functions all work.

    Im having some problems with my code. I suspect the problem lies in my init function and main program. tink my initialisation of portA is wrong and maybe the order in which i call ledindicator function.


    1) The LEDindicator function doesnt work when i join it as a entire program with the LCD funtion. It only works separately. I need it to work together.

    Please assist me in getting the code working. Thanx. The LED is always on and i dont want that.

    i only want the led to glow when an object is detected. eg if detect object then LED glows, if dont detect then led doesnt glow. i dont want the led permanently on. What should i do?


    Code ( (Unknown Language)):
    1. #include <pic.h>
    2. #include "pic.h"
    3. #include "delay.h"
    4. #include "math.h"
    5. #include <stdio.h>
    6. #include <stdlib.h>
    7.  
    8. void FloatToStr(float , char[]);
    9. void DelayMs(unsigned char);
    10. void lcd_cmd(unsigned char);
    11. void lcd_data(unsigned char);
    12. void lcd_clear(void);
    13. void lcd_puts(const char[]);
    14. void lcd_goto_L1(void);
    15. void lcd_goto_L2(void);
    16. void lcd_cursor(unsigned char);
    17. void lcd_init(void);
    18. void enable_interrupt(void);
    19. void interrupt isr(void);
    20. void calc_distance(void);
    21. void init(void);
    22. void read_input_sw(void);
    23. void home_screen(void);
    24. void _40KhzTransmit(void);
    25. void IndicatorLED(void);
    26.  
    27. #define LCD_RS RC0      //LCD RS pin
    28. #define LCD_EN RC1      //LCD EN pin
    29. #define LCD_STROBE()    LCD_EN = 1; asm("nop"); asm("nop"); LCD_EN = 0
    30.  
    31. char input_sw;
    32. unsigned char i,j,k;
    33. char temp[8];
    34.  
    35. ///////////////////////CONVERT FLOAT TO STRING///////////////////
    36. // This function was taken from the CAVR library. It was modified slightly
    37. // to suit our design.
    38. void FloatToStr(float n, char str[])
    39. {
    40. float scale;
    41. unsigned char d,f;
    42. f=0;i=0;
    43. if (n<0.0) {n=-n; str[f]='-'; f++;};
    44. n=n+0.005;
    45. scale=1.0;
    46. while (n>=scale) {scale=scale*10.0; ++i;};
    47. if (i==0) {str[f]='0'; f++;}
    48. else
    49. while (i--)
    50.       {
    51.       scale=floor(0.5+scale/10.0);
    52.       d=(unsigned char) (n/scale);
    53.       str[f]=d+'0';
    54.       n=n-scale*d;
    55.       f++;
    56.       };
    57.  
    58. str[f]='.';
    59. f++;
    60. for (j=0;j<=1;j++) //2 decimal points
    61.       {
    62.       n=n*10.0;
    63.       d=(unsigned char) n;
    64.       str[f]=d+'0';
    65.       n=n-d;
    66.       f++;
    67.       };
    68. str[f]='\0';
    69. }
    70. ///////////////////END CONVERT FLOAT TO STRING///////////////////
    71.  
    72. /////////////////////////////DELAY///////////////////////////////
    73. void DelayMs(unsigned char cnt)
    74. {
    75. #if XTAL_FREQ <= 2MHZ
    76.     do {
    77.         DelayUs(996);
    78.     } while(--cnt);
    79. #endif
    80.  
    81. #if    XTAL_FREQ > 2MHZ
    82.     unsigned char   p;
    83.     do {
    84.         p = 4;
    85.         do {
    86.             DelayUs(250);
    87.         } while(--p);
    88.     } while(--cnt);
    89. #endif
    90. }
    91.  
    92. void DelayS(unsigned char cnt)
    93. {
    94.     for (j=0; j<(cnt*10); j++)
    95.         DelayMs(100);
    96. }
    97. ///////////////////////////DELAY END/////////////////////////////
    98.  
    99. //////////////////////////////LCD SETUP//////////////////////////
    100. /* send a command to the LCD */
    101. void lcd_cmd(unsigned char c)
    102. {
    103.     DelayMs(20); //wait for LCD to be ready
    104.     LCD_RS = 0;  //write instruction
    105.     PORTB = (c & 0xF0); //load upper nibble on LCD data lines
    106.     LCD_STROBE(); //send instruction to LCD
    107.     PORTB = ((c << 4) & 0xF0); //load upper nibble on LCD data lines
    108.     LCD_STROBE(); //send instruction to LCD    
    109. }
    110.  
    111. /* send data to the LCD */
    112. void lcd_data(unsigned char c)
    113. {
    114.     DelayMs(20); //wait for LCD to be ready
    115.     PORTB = 0x00;
    116.     LCD_RS = 1; //write data
    117.     PORTB |= (c & 0xF0); //load upper nibble on LCD data lines    
    118.     LCD_STROBE(); //send instruction to LCD
    119.     PORTB &= 0x00; //load upper nibble on LCD data lines
    120.     PORTB |= ( (c << 4) & 0xF0);
    121.     LCD_STROBE(); //send instruction to LCD
    122. }
    123.  
    124. /*Clear the LCD*/
    125. void lcd_clear(void)
    126. {
    127.     lcd_cmd(0x01); //command to clear LCD
    128. }
    129.  
    130. /*write a string of chars to the LCD*/
    131. void lcd_puts(const char s[])
    132. {
    133.     j = -1;
    134.     while(s[++j]!=('\0')) // send characters until null character reached
    135.         lcd_data(s[j]);
    136. }
    137.  
    138. /*go to beginning of line 1*/
    139. void lcd_goto_L1(void)
    140. {
    141.     lcd_cmd(0b10000000); // command to go to line 1
    142. }
    143.  
    144. /*go to beginning of line 2*/
    145. void lcd_goto_L2(void)
    146. {
    147.     lcd_cmd(0b11000000); // command to go to line 2
    148. }
    149.  
    150. /*move cursor "x" positions to the right*/
    151. void lcd_cursor(unsigned char x)
    152. {
    153.     lcd_cmd(((x)&0x7F)|0x80);
    154. }
    155.  
    156. /*initialise the LCD - put into 4 bit mode*/
    157. void lcd_init(void)
    158. {
    159.     LCD_RS = 0;
    160.     LCD_EN = 0;
    161.     DelayMs(20); //wait for LCD startup
    162.     lcd_cmd(0x28);  // 4-bit mode
    163.     lcd_cmd(0x08);  // display off
    164.     lcd_cmd(0x01);  // clear display
    165.     lcd_cmd(0x0C);  // disp. on, cursor off, cursor blink off
    166.     lcd_cmd(0x06);  // entry mode
    167.     lcd_cmd(0x80);  // initialise DDRAM address to zero
    168. }
    169. //////////////////////////LCD SETUP END//////////////////////////
    170.  
    171. void init(void)
    172. {
    173.     OSCCON|=0x60; //set fosc to 4Mhz
    174.     TRISB=0x00;
    175.     TRISC=0xFC;
    176.     ANSEL=0;    //
    177.     TRISA=0x02; //
    178.     //ANSELH=0x00;
    179.     lcd_init(); //call LCD initialisation
    180.     GIE=0; //disable global interrupt
    181. }
    182.  
    183.  
    184. void home_screen(void)
    185. {
    186.     lcd_clear();
    187.     lcd_goto_L1();
    188.     lcd_puts("INFRARED LIQUID"); //home screen message (line 1)
    189.     lcd_goto_L2();
    190.     lcd_puts("LEVEL DETECTOR"); //home screen message (line 2)
    191.  
    192. }
    193.  
    194. void _40KhzTransmit(void)
    195. {  
    196.     int t=0;
    197.     int cnt=0;
    198.     while(cnt<20)
    199.         {   if (t==0)
    200.             {
    201.                 PORTA=PORTA^0x04;  //toggles RA2
    202.                 DelayUs(13);
    203.                 cnt++;
    204.                 t=1;
    205.             }
    206.             else if (t==1)
    207.             {  
    208.                
    209.                 PORTA=PORTA^0x04;
    210.                 DelayUs(12);
    211.                 cnt++;
    212.                 t=0;
    213.             }
    214.         }
    215. }
    216.  
    217. void IndicatorLED(void)
    218. {
    219.  
    220.  
    221.     while(1)                        //  Loop forever
    222.   {
    223.  
    224.  
    225.     if(RA1==0)              //if receiver IC TSOP48 at pin RA1 detects something then output is active low
    226.             {PORTA=0x01;}   //light up led connected to RA0 if detect object
    227.     else
    228.             {PORTA=0x00;}   //dont light led if no detection
    229.  
    230.   }
    231. }
    232.  
    233. void main(void)
    234. {
    235.     init(); // initialise I/O ports, LCD
    236.     while (1)
    237.     IndicatorLED();
    238.     home_screen();
    239.    
    240. }
    241.  
     
    Last edited: Aug 26, 2009
  10. fantabulous68

    Thread Starter Active Member

    Nov 3, 2007
    51
    4
    i got it to do what i wanted. It just had to swap the order of the two functions in my main program. YAY :). Im a happy girl now

    im going to start working on my timer 1 code. i need the time interval between the reflected and detected infrared light in order to calculate distance from object. :)
     
Loading...