When getting started with microcontroller programming, we usually playing with simple LEDs. A common things is LED blinking, chasing, scrolling etc.
Five colors of 3 mm LEDs. |
In program C, scrolling LEDs made easy by using the binary shift operation. There is no carry bit in this operation. There are two directions of shifting:
- shift left "<<"
- and shift right ">>"
In this example, I use PortD for outputting the shifting LEDs.
PIC16F887 supplied at +5V DC. External crystal of 4 MHz fed the PIC device. SW1 uses to hard reset the MCU. bar graph LED displays the scrolling. |
Source code is written using XC8 C compiler:
#include<xc.h>
// PIC16F887 Configuration Bit Settings
// CONFIG1
#pragma config FOSC = XT
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config MCLRE = ON
#pragma config CP = OFF
#pragma config CPD = OFF
#pragma config BOREN = ON
#pragma config IESO = ON
#pragma config FCMEN = ON
#pragma config LVP = ON
// CONFIG2
#pragma config BOR4V = BOR40V
#pragma config WRT = OFF
/*_XTAL_FREQ use for __delay*/
#define _XTAL_FREQ 4000000
#define blinkRate 100
void main(){
//Clear PortD
PORTD=0x00;
//Set PortD To Output
TRISD=0x00;
while(1){
PORTD=0x01;
while(PORTD!=0){
__delay_ms(blinkRate);
PORTD<<=1;
}
PORTD=0x80;
while(PORTD!=0){
__delay_ms(blinkRate);
PORTD>>=1;
}
}
}
A screen shot of the simulation.
A screen shot of the simulation. PortD outputs to bar graph LED display. |
No comments:
Post a Comment