1. badboyz

    Thread Starter New Member

    May 24, 2009
    3
    0
    Hi Guys..
    I'm new to PIC's micro-controller
    but i have written this code, but i want RC1 and RC2 to output digital signals..

    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3.  
    4. __CONFIG(MCLREN & UNPROTECT & BORDIS & WDTDIS & PWRTEN & INTIO);
    5.  
    6.                         // let RA1 be the signal from the first switch and RA2 be the signal from the second switch.
    7.   int up_1 = 0;        // flag to signal when the door is open due to switch one open
    8.   int up_2 = 0;        // flag to signal when the door is open due to switch two open
    9. int main(){
    10.   TRISC = 0b00000000;
    11.   TRISA = 0b11111111;
    12.  
    13.  while (1){
    14.   if((RA1 == 1) && (RA2 == 0)){
    15.         RC1 = 1;                //Sets The RC1 to digital output 1
    16.         RC2 = 0;                //Sets The RC2 to digital output 0
    17.         up_1 = 1;
    18.         //delay();
    19.     }
    20.     if((RA1 == 0) && (RA2 == 1)){
    21.         RC1 = 1;                //Sets The RC1 to digital output 1
    22.         RC2 = 0;                //Sets The RC2 to digital output 0
    23.         up_2 = 1;
    24.         //delay();
    25.     }
    26.     if((up_1==1) && (RA2 == 1)){
    27.         RC1 = 0;                //Sets The RC1 to digital output 0
    28.         RC2 = 1;                //Sets The RC2 to digital output 1
    29.         //delay();
    30.         up_1 = 0;
    31.     }
    32.     if((up_2==1) && (RA1 == 1)){
    33.         RC1 = 0;                //Sets The RC1 to digital output 0
    34.         RC2 = 1;                //Sets The RC2 to digital output 1
    35.         //delay();
    36.         up_2 = 0;
    37.     }
    38.   }
    39. }
    40. void delay(){                //Some delay...
    41.     unsigned char i,j,k;
    42.     for(i=0;i<0x20;i++)
    43.         for(j=0;j<255;j++)
    44.                 for(k=0;k<255;k++);
    45. }
    46.  
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    You have not set the ansel register correct. Go to the datasheet. And read section 4.1 and 4.2.1 once more:p
    Oh and thing more. If you do not use the MCLR pin, do not enable it. For a beginner that is the best. And the programmer unit take control of it also.
    This is a safe setup to use for the beginner
    Code ( (Unknown Language)):
    1.  
    2. __CONFIG(INTIO & WDTDIS & PWRTDIS & BORDIS & MCLRDIS & FCMEN & IESODIS & UNPROTECT);
    3.  
    Also HI-Tech C have function for delays __delay_ms(), and __delay_us() use those instead for delays. Download the latest version here http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en542849
    By the way PIC16f690 share header file with some other PICs. The file used by 16f690 is pic16f685.h file
     
    Last edited: Sep 11, 2010
  3. badboyz

    Thread Starter New Member

    May 24, 2009
    3
    0
    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3.  
    4. __CONFIG(INTIO & WDTDIS & PWRTDIS & BORDIS & MCLRDIS & FCMEN & IESODIS & UNPROTECT);
    5.  
    6.                         // let RA1 be the signal from the first switch and RA2 be the signal from the second switch.
    7.   int up_1 = 0;        // flag to signal when the door is open due to switch one open
    8.   int up_2 = 0;        // flag to signal when the door is open due to switch two open
    9. int main(){
    10.   TRISA = 0b00000011;
    11.   ANSEL = 0b00000000;
    12.   ANSELH= 0b00000000;
    13.   PORTA = 0b00000000;        // Turn Off all PORTC
    14.  while (1){
    15.   if((RA0 == 1) && (RA1 == 0)){
    16.         RA2 = 1;
    17.         RA3 = 0;
    18.         up_1 = 1;
    19.     }
    20.    
    21.     if((RA1 == 0) && (RA2 == 1)){
    22.         RA2 = 1;
    23.         RA3 = 0;
    24.         up_2 = 1;
    25.     }
    26.    
    27.     if((up_1==1) && (RA2 == 1)){
    28.         RA2 = 0;
    29.         RA3 = 1;
    30.         up_1 = 0;
    31.     }
    32.     if((up_2==1) && (RA1 == 1)){
    33.         RA2 = 0;
    34.         RA3 = 1;
    35.         up_2 = 0;
    36.     }
    37.   }
    38. }
    39.  
    Would that work ?
     
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Setting ANSEL and ANSELH to zero will give digital IO on pins that also is analog input pins. So far so good :) Now you can read RA0, and RA1
    The best programmers do not only master programming. But also debugging. If you use MPLAB you can track down many errors using the software simulator. And if you have at least a PICKIT unit, you can do hardware debugging. Your 16f690 do not support that without some extra hardware. Anyway it is very important to learn how to do single stepping, setting breakpoints, using stimulus etc. As those are your best friends then it comes to programming Microchip has some webinars that you may find useful here http://techtrain.microchip.com/webse...QuickList.aspx
    I would recommend this http://techtrain.microchip.com/webse...aspx?Active=61 and this http://techtrain.microchip.com/webse...spx?Active=137 in the latter order. And perhaps this also http://techtrain.microchip.com/webse...spx?Active=153
     
Loading...