Get Adobe Flash player
English Arabic French German Italian Portuguese Russian Spanish

Did you know?

Median annual earnings for several branches of engineering (U.S., 2009): Electrical: $83,110 Civil: $76,590 Mechanical: $77,020 Computer Hardware: $98,820 Environmental: $77,040 Nuclear: $96,910Biomedical: $78,860

Help us stay online:

small donate

 buttonIn this example RB1 pin is used as digital output pin and RD0 is used as digital input pin. Pin RDO is connected with taster as on picture. RB1 pin can be used as general purpose output pin to drive LED diode directly or as TTL level signal output. When we push button, we change state of diode. Complete source code programmed in dsPic C30 compiler with MPLAB project files is provided and every line has comments with detailed explanation.




Description of task: Change state of diode via RB1 when taster connected to RD0 is pushed.

_FOSC(CSW_FSCM_OFF & XT_PLL4);//frequency of instruction tact is same as frequency of crystal
int state,taster;//flag state of button and flag for button 
void main (void)
ADPCFGbits.PCFG1=1;//configure pin RB1 as digital
TRISBbits.TRISB1=0;// configure pin B1 as output
TRISDbits.TRISD0=1;//// configure pin D0 as input
      if(PORTDbits.RD0==0)//taster with pull-up resistor
                      if(state<21)//state- button must be pushed for some time
                 state=0;//taster was not pushed for enough time
       if(state==20)//if we have button in push-state enough time
       taster=1;//we can tell that button is really pressed, and it is not a glitch
       if(taster==1)//if taster is pressed, we change state of diode 
                      LATBbits.LATB1=~LATBbits.LATB1;//we change state of diode

Complete project and source code can be downloaded here