Wednesday 12 September 2012

My Pic Microcontroller has some LEDs


Lesson 1: Hello LED
This blog post looks at doing nothing more that turning on and off the LEDs connected to port D of the PicKit3 demo board.






#pragma config FOSC = INTIO67
#pragma config WDTEN = OFF, LVP = OFF, MCLRE = OFF
#include "p18f45k20.h"
void main (void)
{
TRISD = 0b01111111; // PORTD bit 7 to output (0); bits 6:0 are inputs (1) LATDbits.LATD7 = 1; // Set LAT register bit 7 to turn on LED
while (1);
}

The TRISD variable is used to access the tri-state for I/O on port D. LATDbits.LATD7 is used to access the seventh bit of port D. The code below uses  LATD instead of LATDbits.LATD7 to achiveve the same affect.


#pragma config FOSC = INTIO67
#pragma config WDTEN = OFF, LVP = OFF, MCLRE = OFF
#include "p18f45k20.h"
void main (void)
{
TRISD = 0b01111111; // PORTD bit 7 to output (0); bits 6:0 are inputs (1)
        LATD = 0x80; while (1);
}

Lesson 2: BLINK LED
Configuration bits are used to set operating modes of enable disable different features of the Pic. The code below uses the configurations bits to set features such as the watch dog timer and the type of oscillator used.


#pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF     // CONFIG1H
#pragma config PWRT = OFF, BOREN = SBORDIS, BORV = 30      // CONFIG2L
#pragma config WDTEN = OFF, WDTPS = 32768      // CONFIG2H
#pragma config MCLRE = OFF, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC  // CONFIG3H
#pragma config STVREN = ON, LVP = OFF, XINST = OFF    // CONFIG4L
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF             // CONFIG5L
#pragma config CPB = OFF, CPD = OFF      // CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF    // CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF       // CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF   // CONFIG7L
#pragma config EBTRB = OFF   // CONFIG7H
#include "p18f45k20.h"
#include "delays.h"
void main (void)
{
TRISD = 0b01111111; // PORTD bit 7 to output (0) ; bits 6:0 are inputs (1) while (1)
{
LATDbits.LATD7 = ~LATDbits.LATD7; // toggle LATD
Delay1KTCYx(500); // Delay 50 x 1000 = 50,000 cycles; 200ms @ 1MHz } }


The Delay1KTCYx(500) instruction is used to create a delay. The delay in this example is 200 ms and  is calculated as follows:

Delay = (No of Cycles to execute an instruction/Frequency)  x Delay in thousands of clock cycles x 1000

Delay = (4/1MHz) x 50 x 1000 = 200ms.

Lesson 3: Rotate LED

#pragma udata (uninitialized data) and #pragma idata (initialized data) are used to allocate memory for static variables in the file register. #pragma code  is used to indicate a section of instructions and #pragma romdata is used for constant data stored in program memory.


/** C O N F I G U R A T I O N   B I T S ******/                                       
/** I N C L U D E S **********************/
/** V A R I A B L E S ********************/#pragma udata // declare statically allocated uninitialized variablesunsigned char LED_Number;  // 8-bit variable
/** D E C L A R A T I O N S *******************************************/// declare constant data in program memory starting at address 0x180#pragma romdata Lesson3_Table = 0x180
const rom unsigned char LED_LookupTable[8] = {0x01, 0x02, 0x04, 0x08,
0x10, 0x20, 0x40, 0x80};
#pragma code    // declare executable instructions
void main (void)
{
    LED_Number = 0;            // initialize    TRISD = 0b00000000;     // PORTD bits 7:0 are all outputs (0)
    while (1)
    {
// use lookup table to output one LED on based on LED_Number value        LATD = LED_LookupTable[LED_Number];  
        LED_Number++;      // rotate display by 1
        if (LED_Number == 8)
            LED_Number = 0;    // go back to LED 0.
        Delay1KTCYx(50);    // Delay 50 x 1000 = 50,000 cycles; 200ms @ 1MHz    } }




Reference
MicroChip PICkit3 Debug Express, PIC18F45k20 - MPLAB C Lessons.



No comments:

Post a Comment