Circuit Negma

C++, C, VB.NET, PCB, Electronics, Circuit Design

PIC18F4520 :: Pyramid Animation [Part 2]

Posted by Circuit Negma on January 20, 2010


Created By: Hussein Nosair

Project:

We are going to start with the main program. The main program is contained inside test.c file.

The following line 4 instruct the compiler to use test.c file as the main file of the project.

Code Snippet
  1. /***********************************************************************/
  2. /*                      DEFINE THE MOTHER CODE                         */
  3. /***********************************************************************/
  4. #define THIS_IS_STACK_APPLICATION               // Define this file to be the main Proj.'s File

The reason for this is because I am storing my project inside Microchip TCPIP stack folder to utilize the built in libraries such as LCD and RS 232 libraries.

Next we are going to include the necessary libraries to this project and define global variables limited to the scope of test.c file only.

   1: /***********************************************************************/

   2: /*                      DEFINE THE LIBRARIES                           */

   3: /***********************************************************************/

   4: #include "Compiler.h"                           // Compiler configuration library

   5: #include "HardwareProfile.h"                    // Hardware configuration library

   6: #include "GenericTypeDefs.h"                    // Global definition library

   7: #include "LCD.h"                                // LCD library

   8:  

   9: /***********************************************************************/

  10: /*                     DEFINE STATIC VARIABLES                         */

  11: /***********************************************************************/

  12: #define TIMER1_LED_DELAY        2               // LED Timeout = 2*1.67sec

  13: #define TMR1_DELAY                5

  14:  

  15: /***********************************************************************/

  16: /*                     DEFINE GLOBAL VARIABLES                         */

  17: /***********************************************************************/

  18: unsigned char LEDTime;                          // Define LED timout

  19: unsigned char TMR1Delay;

 

Include “Compiler.h”

   1: /*********************************************************************

   2:  *

   3:  *                  Compiler and hardware specific definitions

   4:  *

   5:  *********************************************************************

   6:  * FileName:        Compiler.h

   7:  * Dependencies:    None

   8:  ********************************************************************/

   9: #ifndef __COMPILER_H

  10: #define __COMPILER_H

  11:  

  12: // Include proper device header file

  13: #if defined(__18CXX) || defined(HI_TECH_C)    

  14:     // All PIC18 processors

  15:     #if defined(HI_TECH_C)    // HI TECH PICC-18 compiler

  16:         #define __18CXX

  17:         #include <htc.h>

  18:     #else                    // Microchip C18 compiler

  19:         #include <p18cxxx.h>

  20:     #endif

  21: #elif defined(__PIC24F__)    // Microchip C30 compiler

  22:     // PIC24F processor

  23:     #include <p24Fxxxx.h>

  24: #elif defined(__PIC24H__)    // Microchip C30 compiler

  25:     // PIC24H processor

  26:     #include <p24Hxxxx.h>

  27: #elif defined(__dsPIC33F__)    // Microchip C30 compiler

  28:     // dsPIC33F processor

  29:     #include <p33Fxxxx.h>

  30: #elif defined(__dsPIC30F__)    // Microchip C30 compiler

  31:     // dsPIC30F processor

  32:     #include <p30fxxxx.h>

  33: #elif defined(__PIC32MX__)    // Microchip C32 compiler

  34:     #if !defined(__C32__)

  35:         #define __C32__

  36:     #endif

  37:     #include <p32xxxx.h>

  38:     #include <plib.h>

  39: #else

  40:     #error Unknown processor or compiler.  See Compiler.h

  41: #endif

  42:  

  43: #include <stdio.h>

  44: #include <stdlib.h>

  45: #include <string.h>

  46:  

  47:  

  48: // Base RAM and ROM pointer types for given architecture

  49: #if defined(__C32__)

  50:     #define PTR_BASE        DWORD

  51:     #define ROM_PTR_BASE    DWORD

  52: #elif defined(__C30__)

  53:     #define PTR_BASE        WORD

  54:     #define ROM_PTR_BASE    WORD

  55: #elif defined(__18CXX)

  56:     #define PTR_BASE        WORD

  57:     #define ROM_PTR_BASE    unsigned short long

  58: #endif

  59:  

  60:  

  61: // Definitions that apply to all compilers, except C18

  62: #if !defined(__18CXX) || defined(HI_TECH_C)

  63:     #define memcmppgm2ram(a,b,c)    memcmp(a,b,c)

  64:     #define strcmppgm2ram(a,b)        strcmp(a,b)

  65:     #define memcpypgm2ram(a,b,c)    memcpy(a,b,c)

  66:     #define strcpypgm2ram(a,b)        strcpy(a,b)

  67:     #define strncpypgm2ram(a,b,c)    strncpy(a,b,c)

  68:     #define strstrrampgm(a,b)        strstr(a,b)

  69:     #define    strlenpgm(a)            strlen(a)

  70:     #define strchrpgm(a,b)            strchr(a,b)

  71:     #define strcatpgm2ram(a,b)        strcat(a,b)

  72: #endif

  73:  

  74:  

  75: // Definitions that apply to all 8-bit products

  76: // (PIC18)

  77: #if defined(__18CXX)

  78:     #define    __attribute__(a)

  79:  

  80:     #define FAR                         far

  81:  

  82:     // Microchip C18 specific defines

  83:     #if !defined(HI_TECH_C)

  84:         #define ROM                     rom

  85:         #define strcpypgm2ram(a, b)        strcpypgm2ram(a,(far rom char*)b)

  86:     #endif

  87:     

  88:     // HI TECH PICC-18 STD specific defines

  89:     #if defined(HI_TECH_C)

  90:         #define ROM                     const

  91:         #define rom

  92:         #define Nop()                   asm("NOP");

  93:         #define ClrWdt()                asm("CLRWDT");

  94:         #define Reset()                    asm("RESET");

  95:     #endif

  96:     

  97: // Definitions that apply to all 16-bit and 32-bit products

  98: // (PIC24F, PIC24H, dsPIC30F, dsPIC33F, and PIC32)

  99: #else

 100:     #define    ROM                        const

 101:  

 102:     // 16-bit specific defines (PIC24F, PIC24H, dsPIC30F, dsPIC33F)

 103:     #if defined(__C30__)

 104:         #define Reset()                asm("reset")

 105:         #define FAR                 __attribute__((far))

 106:     #endif

 107:  

 108:     // 32-bit specific defines (PIC32)

 109:     #if defined(__C32__)

 110:         #define persistent

 111:         #define far

 112:         #define FAR

 113:         #define Reset()                SoftReset()

 114:         #define ClrWdt()            (WDTCONSET = _WDTCON_WDTCLR_MASK)

 115:         #define Nop()                asm("nop")

 116:     #endif

 117: #endif

 118:  

 119:  

 120:  

 121: #endif

 

#include “HardwareProfile.h”

   1: /*********************************************************************

   2:  *

   3:  *    Hardware specific definitions

   4:  *

   5:  *********************************************************************

   6:  * FileName:        HardwareProfile.h

   7:  * Dependencies:    None

   8:  ********************************************************************/

   9: #ifndef __HARDWARE_PROFILE_H

  10: #define __HARDWARE_PROFILE_H

  11:  

  12: #include "Compiler.h"

  13:  

  14:  

  15: // Choose which hardware profile to compile for here.  See 

  16: // the hardware profiles below for meaning of various options.  

  17: #define PICDEMNET2

  18: //#define YOUR_BOARD

  19:  

  20: // If no hardware profiles are defined, assume that we are using 

  21: // a Microchip demo board and try to auto-select the correct profile

  22: // based on processor selected in MPLAB

  23: #if !defined(PICDEMNET2) && !defined(HPC_EXPLORER) && !defined(PICDEMZ) && !defined(EXPLORER_16) && !defined(PIC24FJ64GA004_PIM) && !defined(DSPICDEM11) && !defined(PICDEMNET2) && !defined(INTERNET_RADIO) && !defined(YOUR_BOARD)

  24:     #if defined(__18F97J60) || defined(_18F97J60) || defined(__18F4520)

  25:         #define PICDEMNET2

  26:     #endif

  27: #endif

  28:  

  29: // Set configuration fuses (but only once)

  30: #if defined(THIS_IS_STACK_APPLICATION)

  31:     #if defined(__18CXX)

  32:         #if defined(__EXTENDED18__)

  33:             #pragma config XINST=ON

  34:         #elif !defined(HI_TECH_C)

  35:             #pragma config XINST=OFF

  36:         #endif

  37:     

  38:         #if defined(__18F97J60) || defined(__18F96J65) || defined(__18F96J60) || defined(__18F87J60) || defined(__18F86J65) || defined(__18F86J60) || defined(__18F67J60) || defined(__18F66J65) || defined(__18F66J60) 

  39:             // PICDEM.net 2 or any other PIC18F97J60 family device

  40:             #pragma config WDT=OFF, FOSC2=ON, FOSC=HSPLL, ETHLED=OFF

  41:         #elif defined(__18F4520) || defined(__18F452)    

  42:             // STANLY KEYPAD

  43:             #pragma config OSC = HSPLL, FCMEN = ON, IESO = ON

  44:             #pragma config PWRT = ON, BOREN = ON, BORV = 1

  45:             #pragma config WDT = OFF, WDTPS = 2048

  46:             #pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC

  47:             #pragma config STVREN = ON, LVP = OFF

  48:         #endif

  49:     #endif

  50: #endif // Prevent more than one set of config fuse definitions

  51:  

  52: // Clock frequency value.

  53: // This value is used to calculate Tick Counter value

  54: #if defined(__18CXX)

  55:     // All PIC18 processors

  56:     #if defined(PICDEMNET2) || defined(INTERNET_RADIO)

  57:         #define GetSystemClock()        (40000000ul) //(41666667ul)      // Hz

  58:         #define GetInstructionClock()    (GetSystemClock()/4)

  59:         #define GetPeripheralClock()    GetInstructionClock()

  60:     #else

  61:         #define GetSystemClock()        (40000000ul)      // Hz

  62:         #define GetInstructionClock()    (GetSystemClock()/4)

  63:         #define GetPeripheralClock()    GetInstructionClock()

  64:     #endif

  65: #endif

  66:  

  67: // Hardware mappings

  68: #if defined(PICDEMNET2) && !defined(HI_TECH_C)

  69: // PICDEM.net 2 (PIC18F97J60 + ENC28J60)   

  70:         

  71:     //Define Backlit keys, Speaker and RS-485 ports

  72:     #define LED_IO      LATCbits.LATC1              // Define RC1 to be LED port   

  73:     #define LED_TRIS    TRISCbits.TRISC1

  74:     

  75:     // LCD I/O pins

  76:     #define LCD_DATA_TRIS        (TRISD)

  77:     #define LCD_DATA_IO            (LATD)

  78:  

  79:     #define LCD_RD_WR_TRIS        (TRISEbits.TRISE2)

  80:     #define LCD_RD_WR_IO        (LATEbits.LATE2)

  81:     #define LCD_RS_TRIS            (TRISEbits.TRISE0)

  82:     #define LCD_RS_IO            (LATEbits.LATE0)

  83:     #define LCD_E_TRIS            (TRISEbits.TRISE1)

  84:     #define LCD_E_IO            (LATEbits.LATE1)

  85:  

  86: #elif defined(YOUR_BOARD)

  87: // Define your own board hardware profile here

  88:  

  89: #else

  90:     #error "Hardware profile not defined.  See available profiles in HardwareProfile.h.  Add the appropriate macro definition to your application configuration file ('TCPIPConfig.h', etc.)"

  91: #endif

  92:  

  93:  

  94: #if defined(__18CXX)    // PIC18

  95:     // UART mapping functions for consistent API names across 8-bit and 16 or 

  96:     // 32 bit compilers.  For simplicity, everything will use "UART" instead 

  97:     // of USART/EUSART/etc.

  98:     #define BusyUART()                BusyUSART()

  99:     #define CloseUART()                CloseUSART()

 100:     #define ConfigIntUART(a)        ConfigIntUSART(a)

 101:     #define DataRdyUART()            DataRdyUSART()

 102:     #define OpenUART(a,b,c)            OpenUSART(a,b,c)

 103:     #define ReadUART()                ReadUSART()

 104:     #define WriteUART(a)            WriteUSART(a)

 105:     #define getsUART(a,b,c)            getsUSART(b,a)

 106:     #define putsUART(a)                putsUSART(a)

 107:     #define getcUART()                ReadUSART()

 108:     #define putcUART(a)                WriteUSART(a)

 109:     #define putrsUART(a)            putrsUSART((far rom char*)a)

 110:  

 111: #endif

 112:  

 113:  

 114: #endif

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: