PIC 16f690

Thread Starter

badboyz

Joined May 24, 2009
3
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..

Rich (BB code):
#include <htc.h>

__CONFIG(MCLREN & UNPROTECT & BORDIS & WDTDIS & PWRTEN & INTIO); 
 
         				// let RA1 be the signal from the first switch and RA2 be the signal from the second switch.
  int up_1 = 0;        // flag to signal when the door is open due to switch one open
  int up_2 = 0;        // flag to signal when the door is open due to switch two open
int main(){
  TRISC = 0b00000000;
  TRISA = 0b11111111;

 while (1){
  if((RA1 == 1) && (RA2 == 0)){
		RC1 = 1;				//Sets The RC1 to digital output 1
		RC2 = 0;				//Sets The RC2 to digital output 0
		up_1 = 1;
		//delay();
	}
	if((RA1 == 0) && (RA2 == 1)){
		RC1 = 1;				//Sets The RC1 to digital output 1
		RC2 = 0;				//Sets The RC2 to digital output 0
		up_2 = 1;
		//delay();
	}
	if((up_1==1) && (RA2 == 1)){
		RC1 = 0;				//Sets The RC1 to digital output 0
		RC2 = 1;				//Sets The RC2 to digital output 1 
		//delay();
		up_1 = 0;
	}
	if((up_2==1) && (RA1 == 1)){
		RC1 = 0;				//Sets The RC1 to digital output 0
		RC2 = 1;				//Sets The RC2 to digital output 1 
		//delay();
		up_2 = 0;
	}
  }
}
void delay(){                //Some delay...
    unsigned char i,j,k;
    for(i=0;i<0x20;i++)
        for(j=0;j<255;j++)
                for(k=0;k<255;k++);
}
 

t06afre

Joined May 11, 2009
5,934
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
Rich (BB code):
__CONFIG(INTIO & WDTDIS & PWRTDIS & BORDIS & MCLRDIS & FCMEN & IESODIS & UNPROTECT);
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:

Thread Starter

badboyz

Joined May 24, 2009
3
Rich (BB code):
#include <htc.h>

__CONFIG(INTIO & WDTDIS & PWRTDIS & BORDIS & MCLRDIS & FCMEN & IESODIS & UNPROTECT);

						// let RA1 be the signal from the first switch and RA2 be the signal from the second switch.
  int up_1 = 0;        // flag to signal when the door is open due to switch one open
  int up_2 = 0;        // flag to signal when the door is open due to switch two open
int main(){
  TRISA = 0b00000011;
  ANSEL = 0b00000000;
  ANSELH= 0b00000000;
  PORTA = 0b00000000;        // Turn Off all PORTC
 while (1){
  if((RA0 == 1) && (RA1 == 0)){
		RA2 = 1;
		RA3 = 0;
		up_1 = 1;
	}
	
	if((RA1 == 0) && (RA2 == 1)){
		RA2 = 1;
		RA3 = 0;
		up_2 = 1;
	}
	
	if((up_1==1) && (RA2 == 1)){
		RA2 = 0;
		RA3 = 1;
		up_1 = 0;
	}
	if((up_2==1) && (RA1 == 1)){
		RA2 = 0;
		RA3 = 1; 
		up_2 = 0;
	}
  }
}
Would that work ?
 

t06afre

Joined May 11, 2009
5,934
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
 
Top