9 Eylül 2017 Cumartesi

Get Started; LED Blinking, Switch ON/OFF a LED, 7-SEGMENT Display, LCD Display

Download MPLABX, XC8 and MCC for Macos-X from Microchip site. They are all free! I am using an old version of Proteus.

 

We will do four examples on the same circuit. You can start with D1, R1 first. Second, you can add SW and D2,R2. Third part you can 7-segment display and finally you can add LCD to the circuit. At each step you can modify the same main.c code, compile and run at Proteus.

I found http://www.studentcompanion.net/en/introduction-to-microchip-xc8-compiler/  tutorials and related youtube videos very useful. I am following their examples with my modifications.

First example is blinking LED.
Select a PIC which is supported by MCC and Proteus such as  PIC18F24K20
Proteus:
Connect a LED to RC5 with a resistor.
MPLAB:
MCC:
select internal 500khz osc
make RC5 out
disable MCLR
Press Generate to create mcc config files. They are automatically added to the project.
I found toggle of RC is a good way with 500ms delay

while(1) {
        IO_RC7_Toggle();
        __delay_ms(500);
}

thats all. Easy start.

Second I will use a switch to control another LED at RC7.
Proteus: connect a SW to RC0.
MCC: make RC7 output to connect LED and RC0 input to control the LED with a SW.
MPLAB:
while(1){
...
    if (IO_RC0_PORT==1) IO_RC5_SetLow();
        else IO_RC5_SetHigh();
...}
I used macros pin_manager.h which is auto created by MCC.

Third: 7-Segment Display
Proteus: 7-Segment green anode connected PORTB
MCC: make PortB pins output
MPLAB:
simple, create a  SEGMENT array for 7-segment LED's
second create a for loop to write 0,1,2,3 to display.

unsigned char pattern, c = 0;
unsigned char SEGMENT[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,
                            0x7D,0x07,0x7F,0x6F};

while(1){
...
        for (c=0; c<=3; c++) {
        pattern=SEGMENT[c];
        PORTB=pattern;
        __delay_ms(500);
        }
...}


4. Lecture: Using 16x2 LCD display
this was the most difficult part. because LCD libraries xlcd.h did not work due to path/directory issues. I looked at internet there were some drivers developed by people. I copied  xlcd.h and delays.h to the project directory, renamed xlcd.h to myxlcd.h    and created myxlcd.c file by copying content of .c source files in XLCD directory and delays function to the end of this file. So you will have three files in your project directory (same directory where main.c exists):

myxlcd.h
myxlcd.c
delays.h

Proteus:
connect LCD to PORTA, power, VSS to ground and VEE to a pot resistor. I am using lower pins which default at xlcd.h
MCC: make portA output and remove analog ticks
MPLAB
include part is below

#include "myxlcd.h"
#include "delays.h"

while(1){
...
        OpenXLCD(FOUR_BIT & LINES_5X7);
        while(BusyXLCD());
        WriteCmdXLCD(0x06);
        WriteCmdXLCD(0x0C);
        putrsXLCD("Hello World");

       for(i=0; i<3; i++){
        SetDDRamAddr(0x40);
        putrsXLCD("           ");
        __delay_ms(1000);
        SetDDRamAddr(0x40); // goto second line
        putrsXLCD("LCD Display");
        __delay_ms(1000);
        }

writing "Hello World" to the first line and writing blinking "LCD Display" to the second line. You can use functions in myxlcd.c file.

download the example



1 yorum:

  1. Learning Pic Programing With Xc8 Compiler: Get Started; Led Blinking, Switch On/Off A Led, 7-Segment Display, Lcd Display >>>>> Download Now

    >>>>> Download Full

    Learning Pic Programing With Xc8 Compiler: Get Started; Led Blinking, Switch On/Off A Led, 7-Segment Display, Lcd Display >>>>> Download LINK

    >>>>> Download Now

    Learning Pic Programing With Xc8 Compiler: Get Started; Led Blinking, Switch On/Off A Led, 7-Segment Display, Lcd Display >>>>> Download Full

    >>>>> Download LINK r9

    YanıtlaSil