PIC18F4520 :: Pyramid Animation [Part 3]
Posted by Circuit Negma on January 20, 2010
Created By: Hussein Nosair
#include GenericTypeDefs.h”
Code Snippet
- /*********************************************************************
- *
- * Generic Type Definitions
- *
- *********************************************************************
- * FileName: GenericTypeDefs.h
- * Dependencies: None
- * Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32
- ********************************************************************/
- #ifndef __GENERIC_TYPE_DEFS_H_
- #define __GENERIC_TYPE_DEFS_H_
- typedef enum _BOOL { FALSE = 0, TRUE } BOOL; // Undefined size
- #ifndef NULL
- #define NULL 0//((void *)0)
- #endif
- #define PUBLIC // Function attributes
- #define PROTECTED
- #define PRIVATE static
- typedef unsigned char BYTE; // 8-bit unsigned
- typedef unsigned short int WORD; // 16-bit unsigned
- typedef unsigned long DWORD; // 32-bit unsigned
- typedef unsigned long long QWORD; // 64-bit unsigned
- typedef signed char CHAR; // 8-bit signed
- typedef signed short int SHORT; // 16-bit signed
- typedef signed long LONG; // 32-bit signed
- typedef signed long long LONGLONG; // 64-bit signed
- /* Alternate definitions */
- typedef voidVOID;
- typedef charCHAR8;
- typedef unsigned charUCHAR8;
- /* Processor & Compiler independent, size specific definitions */
- // To Do: We need to verify the sizes on each compiler. These
- // may be compiler specific, we should either move them
- // to "compiler.h" or #ifdef them for compiler type.
- typedef signed intINT;
- typedef signed charINT8;
- typedef signed short intINT16;
- typedef signed long intINT32;
- typedef signed long longINT64;
- typedef unsigned intUINT;
- typedef unsigned charUINT8;
- typedef unsigned short intUINT16;
- typedef unsigned long intUINT32; // other name for 32-bit integer
- typedef unsigned long longUINT64;
- typedef union _BYTE_VAL
- {
- BYTE Val;
- struct
- {
- unsigned char b0:1;
- unsigned char b1:1;
- unsigned char b2:1;
- unsigned char b3:1;
- unsigned char b4:1;
- unsigned char b5:1;
- unsigned char b6:1;
- unsigned char b7:1;
- } bits;
- } BYTE_VAL, BYTE_BITS;
- typedef union _WORD_VAL
- {
- WORD Val;
- BYTE v[2];
- struct
- {
- BYTE LB;
- BYTE HB;
- } byte;
- struct
- {
- unsigned char b0:1;
- unsigned char b1:1;
- unsigned char b2:1;
- unsigned char b3:1;
- unsigned char b4:1;
- unsigned char b5:1;
- unsigned char b6:1;
- unsigned char b7:1;
- unsigned char b8:1;
- unsigned char b9:1;
- unsigned char b10:1;
- unsigned char b11:1;
- unsigned char b12:1;
- unsigned char b13:1;
- unsigned char b14:1;
- unsigned char b15:1;
- } bits;
- } WORD_VAL, WORD_BITS;
- typedef union _DWORD_VAL
- {
- DWORD Val;
- WORD w[2];
- BYTE v[4];
- struct
- {
- WORD LW;
- WORD HW;
- } word;
- struct
- {
- BYTE LB;
- BYTE HB;
- BYTE UB;
- BYTE MB;
- } byte;
- struct
- {
- WORD_VAL low;
- WORD_VAL high;
- }wordUnion;
- struct
- {
- unsigned char b0:1;
- unsigned char b1:1;
- unsigned char b2:1;
- unsigned char b3:1;
- unsigned char b4:1;
- unsigned char b5:1;
- unsigned char b6:1;
- unsigned char b7:1;
- unsigned char b8:1;
- unsigned char b9:1;
- unsigned char b10:1;
- unsigned char b11:1;
- unsigned char b12:1;
- unsigned char b13:1;
- unsigned char b14:1;
- unsigned char b15:1;
- unsigned char b16:1;
- unsigned char b17:1;
- unsigned char b18:1;
- unsigned char b19:1;
- unsigned char b20:1;
- unsigned char b21:1;
- unsigned char b22:1;
- unsigned char b23:1;
- unsigned char b24:1;
- unsigned char b25:1;
- unsigned char b26:1;
- unsigned char b27:1;
- unsigned char b28:1;
- unsigned char b29:1;
- unsigned char b30:1;
- unsigned char b31:1;
- } bits;
- } DWORD_VAL;
- #define LSB(a) ((a).v[0])
- #define MSB(a) ((a).v[1])
- #define LOWER_LSB(a) ((a).v[0])
- #define LOWER_MSB(a) ((a).v[1])
- #define UPPER_LSB(a) ((a).v[2])
- #define UPPER_MSB(a) ((a).v[3])
- typedef union _QWORD_VAL
- {
- QWORD Val;
- DWORD d[2];
- WORD w[4];
- BYTE v[8];
- struct
- {
- DWORD LD;
- DWORD HD;
- } dword;
- struct
- {
- WORD LW;
- WORD HW;
- WORD UW;
- WORD MW;
- } word;
- struct
- {
- unsigned char b0:1;
- unsigned char b1:1;
- unsigned char b2:1;
- unsigned char b3:1;
- unsigned char b4:1;
- unsigned char b5:1;
- unsigned char b6:1;
- unsigned char b7:1;
- unsigned char b8:1;
- unsigned char b9:1;
- unsigned char b10:1;
- unsigned char b11:1;
- unsigned char b12:1;
- unsigned char b13:1;
- unsigned char b14:1;
- unsigned char b15:1;
- unsigned char b16:1;
- unsigned char b17:1;
- unsigned char b18:1;
- unsigned char b19:1;
- unsigned char b20:1;
- unsigned char b21:1;
- unsigned char b22:1;
- unsigned char b23:1;
- unsigned char b24:1;
- unsigned char b25:1;
- unsigned char b26:1;
- unsigned char b27:1;
- unsigned char b28:1;
- unsigned char b29:1;
- unsigned char b30:1;
- unsigned char b31:1;
- unsigned char b32:1;
- unsigned char b33:1;
- unsigned char b34:1;
- unsigned char b35:1;
- unsigned char b36:1;
- unsigned char b37:1;
- unsigned char b38:1;
- unsigned char b39:1;
- unsigned char b40:1;
- unsigned char b41:1;
- unsigned char b42:1;
- unsigned char b43:1;
- unsigned char b44:1;
- unsigned char b45:1;
- unsigned char b46:1;
- unsigned char b47:1;
- unsigned char b48:1;
- unsigned char b49:1;
- unsigned char b50:1;
- unsigned char b51:1;
- unsigned char b52:1;
- unsigned char b53:1;
- unsigned char b54:1;
- unsigned char b55:1;
- unsigned char b56:1;
- unsigned char b57:1;
- unsigned char b58:1;
- unsigned char b59:1;
- unsigned char b60:1;
- unsigned char b61:1;
- unsigned char b62:1;
- unsigned char b63:1;
- } bits;
- } QWORD_VAL;
- #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 infinitly2: {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: else17: {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 File21:22: /***********************************************************************/23: /* DEFINE THE LIBRARIES */24: /***********************************************************************/25: #include "Compiler.h" // Compiler configuration library26: #include "HardwareProfile.h" // Hardware configuration library27: #include "GenericTypeDefs.h" // Global definition library28: #include "LCD.h" // LCD library29:30: /***********************************************************************/31: /* DEFINE STATIC VARIABLES */32: /***********************************************************************/33: #define TIMER1_LED_DELAY 2 // LED Timeout = 2*1.67sec34: #define TMR1_DELAY 535:36: /***********************************************************************/37: /* DEFINE GLOBAL VARIABLES */38: /***********************************************************************/39: unsigned char LEDTime; // Define LED timout40: 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 subroutine55:56: /***********************************************************************/57: /* PIC18 Interrupt Service Routines */58: /***********************************************************************/59: #pragma interruptlow LowISR60: 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 HighISR77: void HighISR(void){}78: #pragma code lowVector=0x1879: void LowVector(void){_asm goto LowISR _endasm}80: #pragma code highVector=0x881: void HighVector(void){_asm goto HighISR _endasm}82: #pragma code // Return to default code section83: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 hardware94:95: /************** PREPARE THE LCD ****************/96: LCDErase(); // Clear the LCD97: 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 infinitly113: {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: else128: {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 Pins158: ADCON1 = 0x0F; // Analog pins to digital159: CMCON = 0x00; //0xCF;160: CVRCON = 0x00;161:162: // Enable internal PORTB pull-ups163: INTCON2bits.RBPU = 1;164:165: //Enable Interrupts166: //----------------------167: RCONbits.IPEN = 1; // Enable interrupt priorities168: INTCONbits.GIEH = 1;169: INTCONbits.GIEL = 1;170:171: // Timer 1 = 52.428msec172: //------------------------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 selection182: //------------------------183: OSCTUNE = 0x00; //<-----------184:185:186: //LEDs187: //------------------------188: LED_TRIS = 0;189: LED_IO = 0;190:191:192: // LCD193: //------------------------194: LCD_DATA_TRIS = 0x0F; // Set Data Bus Direction to output195: LCD_RD_WR_TRIS = 0; // Set R/W pin direction to output196: LCD_RS_TRIS = 0; // Set Reset pin direction to output197: LCD_E_TRIS = 0; // Set Enable pin direction to output198: DelayMs(50); // wait for 15ms to allow the voltage to rise to moe than 4.5V199: ClrWdt();200:201:202: // Initialize LCD203: LCDInit();204: }
EEWeb Electronics Forum