2016年7月25日 星期一

pic 18f4620 led example

Ref : https://electrosome.com/led-blinking-pic/



pin  11  和 pin32 要接 vdd

pin  12  和 pin pin31 要接 gnd..

不然就會發生device id 為0 的 case


#include<p18f4550.h>                                                // Include Header for PIC18f455        
  

       /* ******COMPILER DIRECTIVES FOR CHIP CONFIGURATION BITS ***   */
#pragma config PLLDIV = 5 , CPUDIV = OSC1_PLL2 , USBDIV = 2    // You can write this way
// OR
#pragma config FOSC = INTOSCIO_EC
#pragma config FCMEN = OFF                                 // OR this way
#pragma config BORV = 3
#pragma config WDT = OFF
#pragma config CPB = OFF
#pragma config CPD = OFF
/*  ***************  TIMER *************** */
       void delayzz(void)
        {              int i, j;
                        for(i=0;i<5000;i++)
                {
                          for(j=0;j<2;j++)
                      {           /* Well its Just a Timer */            }    }   }
                           
                /* ****************** MAIN ****************** */
      void main(void)
         {
                        TRISB = 0 ;                  // PORT B Setting: Set all the pins in port B to Output.
   
           while(1)
                            {
                                LATBbits.LATB0 = 1;   // RB-1 to High  
                                LATBbits.LATB1 = 1;   // RB-1 to High
                                 delayzz();
                                LATBbits.LATB0 = 0;    // RB-0 to LOW
                                LATBbits.LATB1 = 0;    // RB-0 to LOW
                                 delayzz();
                              }
        }
/* THE END */

Next step is to compile the code.
After the project is built the output files will be dumped into the same project folder, if a separate output folder is not defined for saving all the compiled files.
Now you can use your Programmer to burn the output .hex into the microcontroller.

-Define the header files for pic18f4550.
-Set the Microcontroller configuration settings with “compiler directives”.
-Define PORT settings:- The pins you want to use for blinking. Set the pin to output.
-Then set the timer:- For the time delay in-between the blinks.
-Define Pins to set as ON or OFF with a delay in-between.
-And finally make a loop where the main code will keep executing.

#include


Like most of the programming language first stage is to include/Import header files that we need to define. We are going to code a pic18f4550 hence we are going to include <p18f4550> header that contains all the respective settings and definitions of the microcontroller. 


#Pragma config [Compiler Directive]– Setting the Configuration bits.


Next stage is the pic18f4550 Configuration Bit settings. We need to set the chip configuration, settings for the pic18f4550, which is going to define the Configuration bits for the pic18f4550. For example: whether to use an internal or external oscillator, whether to keep watchdog timer on or off etc. All such settings are handled in Configuration bits setting in the beginning of the code block. If we don’t define the values then the microcontroller will take the default values which might lead to some trouble.
These configuration bits can be assigned with a Compiler directive “#pragma config ”
for example
             #pragma config WDT = ON // watchdog timer on.

For each Configuration Bits, you can write a new line or you can write multiple Chip config bit setting on single #pragma config Directive.
             #pragma config FCMEN = OFF  // Each line with individual setting
             #pragma config IESO = OFFF                             

OR
             #pragma config FCMEN = OFF , IESO = OFFF  //Single line with multiple settings.



You can find the all the Configuration bit settings in the Mplab notes.
-On the top menu on MPLAB IDE click on Help  >>  Topics..  >> PIC18F Config Settings 
-From the left pane go to PIC18F4xxx Configuration Settings and then select PIC18F4550. It would list you all the configuration settings for pic18f4550.

 It would list you all the configuration settings for pic18f4550.

PORT settings: TRISB = 0xF0


IF you want to use any pin in the microcontroller for any operation then you have to first set the pin to either for an input or out put. So you have to define the corresponding PORT to either Input or out put.
  TRISB = 0xF0 ;     // Set pins 7,6,5,4 as input and 3,2,1,0 as Output.
Set pins according to the binary equivalent of the HEX value (0xF0), which is 11110000, Hence first 4 bits from RB0 would be set to output and rest of the pins starting from RB4 would become input.

PORTS

A typical pic18f4550 has 40 pins in it (if it’s a DIP). These 40 pins are divided (classified) into a Logical name PORTS. There are 5 ports in a pic18f4550, PORTB is one of such ports.
Each port is associated with few pins. These ports are accessed by some registersTRISx register is one of such register to access the corresponding port.


TRISB

TRISB Register is used to access port B, which is to define the pins under port B as input or output. Port B has 8 pins, From RB0 to RB7. Check the pin diagram.
For example:
TRISB = 0xF0 ;   // Set pins in Port B  11110000.
Instead of TRISB = 0xF0 ; you can also write
TRISB = 0;
For Setting all the pins in PORTB to input
TRISB = 1;   // Set all the pins in port B to input.



Instead of assigning each pin one by one, you can reduce the coding by defining the entire TRISB to output, only if all the pins are for same purpose.
 Accessing Individual pins
In real time programming there are certain situations where you would want some pins to work as input and some as output in the same PORT.
At such situation you can set individual pins as either input or output.
                TRISBbits.TRISB0 = 0;       //  Set only pin RB0 to output
                TRISBbits.TRISB1 = 1;       //  Set only pin RB1 to Input

In the figure above the pins from same PORTB , RB0-RB3 is set to output, and RB4-RB7 is defined as input. Incase of individual pin settings you have to specifically mention the individual pin.  The pin not defined will be left unused.
TRISB was just an example, same method can be used to gain access to other ports also (PORT A, B, C, D, E). Review the port settings for various Ports and pins associated with them in the datasheet.

void main(void)


The void main is the main section from where the code actually starts to execute. All the function prototypes that are defined outside the main can be called from inside. As like indelayzz() in the code above where we defined it outside the main code block.
The first stage is to Set the port settings, as shown above in PORTS subtopic
The while(1) defined inside is simple endless loop that keep executing the block inside the braces.
For our blinking project we have defined two led's to blink simultaneously together usingLATB Register
                              LATBbits.LATB0 = 1;   // RB-0 to High
                              LATBbits.LATB1 = 1;   // RB-1 to High
                                 delayzz(); // calling the delay function.
                              LATBbits.LATB0 = 0;    // RB-0 to LOW
                              LATBbits.LATB1 = 0;    // RB-1 to LOW

The code can be easily modified to blink alternatively to emit a dipping effect.
          
                             LATBbits.LATB0 = 1;   // RB-0 to High
                             LATBbits.LATB1 = 0;   // RB-1 to LOW
                                 delayzz(); // calling the delay function.
                             LATBbits.LATB0 = 0;    // RB-0 to LOW
                             LATBbits.LATB1 = 1;    // RB-1 to High
                                 delayzz(); // calling the delay function.
For more information Visit my post on PIC18F4550


2.1 Oscillator Types
PIC18F2525/2620/4525/4620 devices can be operated
in ten different oscillator modes. The user can program
the Configuration bits, FOSC3:FOSC0, in Configuration
Register 1H to select one of these ten modes:
1. LP Low-Power Crystal
2. XT Crystal/Resonator
3. HS High-Speed Crystal/Resonator
4. HSPLL High-Speed Crystal/Resonator
with PLL Enabled
5. RC External Resistor/Capacitor with
FOSC/4 Output on RA6
6. RCIO External Resistor/Capacitor with I/O
on RA6
7. INTIO1 Internal Oscillator with FOSC/4 Output
on RA6 and I/O on RA7
8. INTIO2 Internal Oscillator with I/O on RA6
and RA7
9. EC External Clock with FOSC/4 Output
10. ECIO External Clock with I/O on RA6



SYNC BRG16 BRGH
0            0            0                 8-bit/Asynchronous FOSC/[64 (n + 1)]


example :


For a device with FOSC of 16 MHz, desired baud rate of 9600, Asynchronous mode, 8-bit BRG:
Desired Baud Rate = FOSC/(64 ([SPBRGH:SPBRG] + 1))
Solving for SPBRGH:SPBRG:
X = ((FOSC/Desired Baud Rate)/64) – 1
= ((16000000/9600)/64) – 1
= [25.042] = 25
Calculated Baud Rate = 16000000/(64 (25 + 1))
= 9615






INTIO1 Internal Oscillator with FOSC/4 Output
on RA6 and I/O on RA7
8. INTIO2 Internal Oscillator with I/O on RA6
and RA7



Internal Oscillator Block
The PIC18F2525/2620/4525/4620 devices include an
internal oscillator block which generates two different
clock signals; either can be used as the microcontroller’s
clock source. This may eliminate the need for external
oscillator circuits on the OSC1 and/or OSC2 pins.



The main output (INTOSC) is an 8 MHz clock source,
which can be used to directly drive the device clock. It
also drives a postscaler, which can provide a range of
clock frequencies from 31 kHz to 4 MHz. The INTOSC
output is enabled when a clock frequency from 125 kHz
to 8 MHz is selected.


INTIO MODES
Using the internal oscillator as the clock source
eliminates the need for up to two external oscillator
pins, which can then be used for digital I/O. Two distinct
configurations are available:
• In INTIO1 mode, the OSC2 pin outputs FOSC/4,
while OSC1 functions as RA7 for digital input and
output.
• In INTIO2 mode, OSC1 functions as RA7 and
OSC2 functions as RA6, both for digital input and
output.


4 MHz or 8 MHz (OSCCON<6:4> = 111
or 110)




沒有留言:

張貼留言