Circuit Negma

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

PIC18F4520 :: Pyramid Animation [Part 3]

Posted by Circuit Negma on January 20, 2010


Created By: Hussein Nosair

#include GenericTypeDefs.h”

Code Snippet
  1. /*********************************************************************
  2. *
  3. *                  Generic Type Definitions
  4. *
  5. *********************************************************************
  6. * FileName:        GenericTypeDefs.h
  7. * Dependencies:    None
  8. * Processor:       PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32
  9. ********************************************************************/
  10.  
  11. #ifndef __GENERIC_TYPE_DEFS_H_
  12. #define __GENERIC_TYPE_DEFS_H_
  13.  
  14. typedef enum _BOOL { FALSE = 0, TRUE } BOOL;    // Undefined size
  15.  
  16. #ifndef NULL
  17. #define NULL    0//((void *)0)
  18. #endif
  19.  
  20. #define    PUBLIC                                  // Function attributes
  21. #define PROTECTED
  22. #define PRIVATE   static
  23.  
  24. typedef unsigned char        BYTE;                // 8-bit unsigned
  25. typedef unsigned short int    WORD;                // 16-bit unsigned
  26. typedef unsigned long        DWORD;                // 32-bit unsigned
  27. typedef unsigned long long    QWORD;                // 64-bit unsigned
  28. typedef signed char            CHAR;                // 8-bit signed
  29. typedef signed short int    SHORT;                // 16-bit signed
  30. typedef signed long            LONG;                // 32-bit signed
  31. typedef signed long long    LONGLONG;            // 64-bit signed
  32.  
  33. /* Alternate definitions */
  34.                 typedef voidVOID;
  35.  
  36.                 typedef charCHAR8;
  37.        typedef unsigned charUCHAR8;
  38.  
  39. /* Processor & Compiler independent, size specific definitions */
  40. // To Do:  We need to verify the sizes on each compiler.  These
  41. //         may be compiler specific, we should either move them
  42. //         to "compiler.h" or #ifdef them for compiler type.
  43.           typedef signed intINT;
  44.          typedef signed charINT8;
  45.     typedef signed short intINT16;
  46.      typedef signed long intINT32;
  47.     typedef signed long longINT64;
  48.  
  49.         typedef unsigned intUINT;
  50.        typedef unsigned charUINT8;
  51.   typedef unsigned short intUINT16;
  52.    typedef unsigned long intUINT32;  // other name for 32-bit integer
  53.   typedef unsigned long longUINT64;
  54.  
  55. typedef union _BYTE_VAL
  56. {
  57.     BYTE Val;
  58.     struct
  59.     {
  60.         unsigned char b0:1;
  61.         unsigned char b1:1;
  62.         unsigned char b2:1;
  63.         unsigned char b3:1;
  64.         unsigned char b4:1;
  65.         unsigned char b5:1;
  66.         unsigned char b6:1;
  67.         unsigned char b7:1;
  68.     } bits;
  69. } BYTE_VAL, BYTE_BITS;
  70.  
  71. typedef union _WORD_VAL
  72. {
  73.     WORD Val;
  74.     BYTE v[2];
  75.     struct
  76.     {
  77.         BYTE LB;
  78.         BYTE HB;
  79.     } byte;
  80.     struct
  81.     {
  82.         unsigned char b0:1;
  83.         unsigned char b1:1;
  84.         unsigned char b2:1;
  85.         unsigned char b3:1;
  86.         unsigned char b4:1;
  87.         unsigned char b5:1;
  88.         unsigned char b6:1;
  89.         unsigned char b7:1;
  90.         unsigned char b8:1;
  91.         unsigned char b9:1;
  92.         unsigned char b10:1;
  93.         unsigned char b11:1;
  94.         unsigned char b12:1;
  95.         unsigned char b13:1;
  96.         unsigned char b14:1;
  97.         unsigned char b15:1;
  98.     } bits;
  99. } WORD_VAL, WORD_BITS;
  100.  
  101. typedef union _DWORD_VAL
  102. {
  103.     DWORD Val;
  104.     WORD w[2];
  105.     BYTE v[4];
  106.     struct
  107.     {
  108.         WORD LW;
  109.         WORD HW;
  110.     } word;
  111.     struct
  112.     {
  113.         BYTE LB;
  114.         BYTE HB;
  115.         BYTE UB;
  116.         BYTE MB;
  117.     } byte;
  118.     struct
  119.     {
  120.         WORD_VAL low;
  121.         WORD_VAL high;
  122.     }wordUnion;
  123.     struct
  124.     {
  125.         unsigned char b0:1;
  126.         unsigned char b1:1;
  127.         unsigned char b2:1;
  128.         unsigned char b3:1;
  129.         unsigned char b4:1;
  130.         unsigned char b5:1;
  131.         unsigned char b6:1;
  132.         unsigned char b7:1;
  133.         unsigned char b8:1;
  134.         unsigned char b9:1;
  135.         unsigned char b10:1;
  136.         unsigned char b11:1;
  137.         unsigned char b12:1;
  138.         unsigned char b13:1;
  139.         unsigned char b14:1;
  140.         unsigned char b15:1;
  141.         unsigned char b16:1;
  142.         unsigned char b17:1;
  143.         unsigned char b18:1;
  144.         unsigned char b19:1;
  145.         unsigned char b20:1;
  146.         unsigned char b21:1;
  147.         unsigned char b22:1;
  148.         unsigned char b23:1;
  149.         unsigned char b24:1;
  150.         unsigned char b25:1;
  151.         unsigned char b26:1;
  152.         unsigned char b27:1;
  153.         unsigned char b28:1;
  154.         unsigned char b29:1;
  155.         unsigned char b30:1;
  156.         unsigned char b31:1;
  157.     } bits;
  158. } DWORD_VAL;
  159.  
  160. #define LSB(a)          ((a).v[0])
  161. #define MSB(a)          ((a).v[1])
  162.  
  163. #define LOWER_LSB(a)    ((a).v[0])
  164. #define LOWER_MSB(a)    ((a).v[1])
  165. #define UPPER_LSB(a)    ((a).v[2])
  166. #define UPPER_MSB(a)    ((a).v[3])
  167.  
  168. typedef union _QWORD_VAL
  169. {
  170.     QWORD Val;
  171.     DWORD d[2];
  172.     WORD w[4];
  173.     BYTE v[8];
  174.     struct
  175.     {
  176.         DWORD LD;
  177.         DWORD HD;
  178.     } dword;
  179.     struct
  180.     {
  181.         WORD LW;
  182.         WORD HW;
  183.         WORD UW;
  184.         WORD MW;
  185.     } word;
  186.     struct
  187.     {
  188.         unsigned char b0:1;
  189.         unsigned char b1:1;
  190.         unsigned char b2:1;
  191.         unsigned char b3:1;
  192.         unsigned char b4:1;
  193.         unsigned char b5:1;
  194.         unsigned char b6:1;
  195.         unsigned char b7:1;
  196.         unsigned char b8:1;
  197.         unsigned char b9:1;
  198.         unsigned char b10:1;
  199.         unsigned char b11:1;
  200.         unsigned char b12:1;
  201.         unsigned char b13:1;
  202.         unsigned char b14:1;
  203.         unsigned char b15:1;
  204.         unsigned char b16:1;
  205.         unsigned char b17:1;
  206.         unsigned char b18:1;
  207.         unsigned char b19:1;
  208.         unsigned char b20:1;
  209.         unsigned char b21:1;
  210.         unsigned char b22:1;
  211.         unsigned char b23:1;
  212.         unsigned char b24:1;
  213.         unsigned char b25:1;
  214.         unsigned char b26:1;
  215.         unsigned char b27:1;
  216.         unsigned char b28:1;
  217.         unsigned char b29:1;
  218.         unsigned char b30:1;
  219.         unsigned char b31:1;
  220.         unsigned char b32:1;
  221.         unsigned char b33:1;
  222.         unsigned char b34:1;
  223.         unsigned char b35:1;
  224.         unsigned char b36:1;
  225.         unsigned char b37:1;
  226.         unsigned char b38:1;
  227.         unsigned char b39:1;
  228.         unsigned char b40:1;
  229.         unsigned char b41:1;
  230.         unsigned char b42:1;
  231.         unsigned char b43:1;
  232.         unsigned char b44:1;
  233.         unsigned char b45:1;
  234.         unsigned char b46:1;
  235.         unsigned char b47:1;
  236.         unsigned char b48:1;
  237.         unsigned char b49:1;
  238.         unsigned char b50:1;
  239.         unsigned char b51:1;
  240.         unsigned char b52:1;
  241.         unsigned char b53:1;
  242.         unsigned char b54:1;
  243.         unsigned char b55:1;
  244.         unsigned char b56:1;
  245.         unsigned char b57:1;
  246.         unsigned char b58:1;
  247.         unsigned char b59:1;
  248.         unsigned char b60:1;
  249.         unsigned char b61:1;
  250.         unsigned char b62:1;
  251.         unsigned char b63:1;
  252.     } bits;
  253. } QWORD_VAL;
  254.  
  255. #endif //__GENERIC_TYPE_DEFS_H_

#include “LCD.h”

Unfortunately, I cannot show this library as it does not belong to me. However, I am using RD4, RD5, RD6, and RD7 for data lines between LCD hardware module and PIC uC.

You could modify and use the LCDBlocking.h and LCDBlocking.c files included with Microchip TCPIP stack.

Moving on next to defining the pyramid custom characters. The pyramid consist of 8 custom characters, defined with an 8×8 array.

   1: const unsigned char p2[8][8] = {{0,0,0,0,0,0,0,0x1F},

   2:                                  {0,0,0,0,0,0,0x1F,0x1F},

   3:                                  {0,0,0,0,0,0x1F,0x1F,0x1F},

   4:                                  {0,0,0,0,0x1F,0x1F,0x1F,0x1F},

   5:                                  {0,0,0,0x1F,0x1F,0x1F,0x1F,0x1F},

   6:                                  {0,0,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},

   7:                                  {0,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},

   8:                                  {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F}};

 

Once the characters are defined in the code, we need to load those characters into the LCD CGRAM. To do this the following code was used to upload those characters onto the LCD.

   1: /************* LOAD PYRAMID INTO LCD uC ****************/

   2: for(i=0; i<8; i++)

   3: {

   4:     LCDCustomChar(i, p2[i]);

   5: }

   6:  

   7: i = 0;

   8: j = 1;

   9: LED_IO = 0;

  10: LCD_Move(2,2);

  11:     

 

And next, we need the algorithm that will execute the pyramid and perform the animation.

   1: while(1)                                    // Loop infinitly

   2: {

   3:     if (!TMR1Delay)

   4:     {

   5:         //LED_IO ^= 1;

   6:         

   7:         if (i<8)

   8:         {

   9:             LCDReadCustomChar(i++);

  10:         }

  11:         else if(i<17)

  12:         {                

  13:             LCDReadCustomChar(16-i);

  14:             i++;

  15:         }

  16:         else

  17:         {

  18:             LCDErase();

  19:             if(j > 4)

  20:             {

  21:                 j = 1;

  22:             }    

  23:             LCD_Move(j++,2);        

  24:             i = 0;

  25:         }

  26:         

  27:         TMR1Delay = TMR1_DELAY;

  28:     }

  29: }

Here is the complete code of test.c file

 

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

   2: /*                      PoE Keypad Test Program                        */

   3: /*                                                                     */

   4: /*  Created By: Hussein Nosair                                         */

   5: /*  Date      : 01/27/2009                                             */

   6: /*  Project   : PoE_kp_testing                                         */

   7: /*  Proj. Path: C:\codes\PoE_kp_testing                                */

   8: /*  File Name : test.c                                                 */

   9: /*                                                                     */

  10: /* Author               Date      Comment                              */

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

  12: /* Hussein Nosair        1/14/09      test.c created                       */

  13: /* Hussein Nosair       1/07/09   test.c Fixed configurations bits     */

  14: /* Hussein Nosair       1/27/09   test.c Fixed bug in main program     */

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

  16:  

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

  18: /*                      DEFINE THE MOTHER CODE                         */

  19: /***********************************************************************/

  20: #define THIS_IS_STACK_APPLICATION               // Define this file to be the main Proj.'s File

  21:  

  22: /***********************************************************************/

  23: /*                      DEFINE THE LIBRARIES                           */

  24: /***********************************************************************/

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

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

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

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

  29:  

  30: /***********************************************************************/

  31: /*                     DEFINE STATIC VARIABLES                         */

  32: /***********************************************************************/

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

  34: #define TMR1_DELAY                5

  35:  

  36: /***********************************************************************/

  37: /*                     DEFINE GLOBAL VARIABLES                         */

  38: /***********************************************************************/

  39: unsigned char LEDTime;                          // Define LED timout

  40: unsigned char TMR1Delay;

  41:  

  42: const unsigned char p2[8][8] = {{0,0,0,0,0,0,0,0x1F},

  43:                                  {0,0,0,0,0,0,0x1F,0x1F},

  44:                                  {0,0,0,0,0,0x1F,0x1F,0x1F},

  45:                                  {0,0,0,0,0x1F,0x1F,0x1F,0x1F},

  46:                                  {0,0,0,0x1F,0x1F,0x1F,0x1F,0x1F},

  47:                                  {0,0,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},

  48:                                  {0,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},

  49:                                  {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F}};

  50:  

  51: /***********************************************************************/

  52: /*                     DEFINE PRIVATE SUBROUTINES                      */

  53: /***********************************************************************/

  54: void InitializeBoard(void);                     // Define Board's hardware initialization subroutine

  55:  

  56: /***********************************************************************/

  57: /*                     PIC18 Interrupt Service Routines                */

  58: /***********************************************************************/

  59:     #pragma interruptlow LowISR

  60:     void LowISR(void)

  61:     {

  62:            if (PIR1bits.TMR1IF)

  63:         {

  64:             if(TMR1Delay)

  65:             {

  66:                 TMR1Delay--;

  67:             }    

  68:                         

  69:             PIR1bits.TMR1IF = 0;

  70:             TMR1H = 0x00;

  71:             TMR1L = 0x00;

  72:         }        

  73:  

  74:     }

  75:     

  76:     #pragma interruptlow HighISR

  77:     void HighISR(void){}

  78:     #pragma code lowVector=0x18

  79:     void LowVector(void){_asm goto LowISR _endasm}

  80:     #pragma code highVector=0x8

  81:     void HighVector(void){_asm goto HighISR _endasm}

  82:     #pragma code // Return to default code section

  83:  

  84: /***********************************************************************/

  85: /*                     MAIN PROGRAM ROUTINE                            */

  86: /***********************************************************************/

  87: void main(void)

  88: { 

  89:     unsigned char i;

  90:     unsigned char j;

  91:           

  92:             /************** INITIALIZE THE BOARD ************/

  93:             InitializeBoard();                  // Initialize board and PIC hardware

  94:  

  95:             /************** PREPARE THE LCD ****************/

  96:             LCDErase();                         // Clear the LCD

  97:             LCDHome();

  98:  

  99:             /************* LOAD PYRAMID INTO LCD uC ****************/

 100:             for(i=0; i<8; i++)

 101:             {

 102:                 LCDCustomChar(i, p2[i]);

 103:             }

 104:  

 105:             i = 0;

 106:             j = 1;

 107:             LED_IO = 0;

 108:             LCD_Move(2,2);

 109:     

 110:     

 111:     /********************** MAIN PROGRAM INFINITE LOOP *******************/

 112:     while(1)                                    // Loop infinitly

 113:     {

 114:         if (!TMR1Delay)

 115:         {

 116:             //LED_IO ^= 1;

 117:             

 118:             if (i<8)

 119:             {

 120:                 LCDReadCustomChar(i++);

 121:             }

 122:             else if(i<17)

 123:             {                

 124:                 LCDReadCustomChar(16-i);

 125:                 i++;

 126:             }

 127:             else

 128:             {

 129:                 LCDErase();

 130:                 if(j > 4)

 131:                 {

 132:                     j = 1;

 133:                 }    

 134:                 LCD_Move(j++,2);        

 135:                 i = 0;

 136:             }

 137:             

 138:             TMR1Delay = TMR1_DELAY;

 139:         }

 140:     }       

 141: }

 142:  

 143: /********************************************************************

 144: *       Function Name:  InitializeBoard                             *

 145: *       Return Value:   None                                        *

 146: *       Parameters:     void                                        *

 147: *       Description:    This routine initialize the on board's      *

 148: *                       modules and microprocessor.                 *

 149: ********************************************************************/

 150: void InitializeBoard(void)

 151: {

 152:     RCON    = 0x80;

 153:     INTCON  = 0xE0;

 154:     INTCON2 = 0x84;

 155:     INTCON3 = 0x00;

 156:  

 157:     // Digital Pins

 158:     ADCON1 = 0x0F;              // Analog pins to digital

 159:     CMCON = 0x00; //0xCF;

 160:     CVRCON = 0x00;

 161:  

 162:     // Enable internal PORTB pull-ups

 163:     INTCON2bits.RBPU = 1;

 164:     

 165:     //Enable Interrupts

 166:     //----------------------

 167:     RCONbits.IPEN = 1;            // Enable interrupt priorities

 168:     INTCONbits.GIEH = 1;

 169:     INTCONbits.GIEL = 1;

 170:     

 171:     // Timer 1 = 52.428msec

 172:     //------------------------

 173:     PIR1bits.TMR1IF = 0;

 174:     PIE1bits.TMR1IE = 1;

 175:     IPR1bits.TMR1IP = 0;

 176:     T1CON = 0xB1;

 177:     TMR1H = 0x00;

 178:     TMR1L = 0x00;

 179:     TMR1Delay = TMR1_DELAY;

 180:     

 181:     // Oscillator selection

 182:     //------------------------

 183:     OSCTUNE = 0x00;             //<-----------

 184:     

 185:     

 186:     //LEDs

 187:     //------------------------

 188:     LED_TRIS = 0;

 189:     LED_IO = 0;

 190:  

 191:     

 192:     // LCD

 193:     //------------------------

 194:     LCD_DATA_TRIS = 0x0F;   // Set Data Bus Direction to output

 195:     LCD_RD_WR_TRIS  = 0;    // Set R/W pin direction to output

 196:     LCD_RS_TRIS     = 0;    // Set Reset pin direction to output

 197:     LCD_E_TRIS      = 0;    // Set Enable pin direction to output

 198:     DelayMs(50);            // wait for 15ms to allow the voltage to  rise to moe than 4.5V

 199:     ClrWdt();    

 200:  

 201:      

 202:     // Initialize LCD

 203:     LCDInit();    

 204: }

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: