Mohamad Atip
Newbie level 1
- Joined
- Aug 19, 2014
- Messages
- 1
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 81
help me please....
i want make project automatic shoe dryer...but i dont know how to interfacing with pic18f4550.
this is my program:
i want make project automatic shoe dryer...but i dont know how to interfacing with pic18f4550.
this is my program:
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 #include <p18f4550.h> #include "delays.h" // Configuration bits //============================================================================= #pragma config PLLDIV = 5 // (20 MHz crystal), divide with 5 to get 4MHz and go through 96MHz PLL #pragma config CPUDIV = OSC1_PLL2 //CPU use 96MHZ/ 2 = 48MHz #pragma config USBDIV = 2 //USB clock at 96MHz / 2 = 48 MHz #pragma config FOSC = HSPLL_HS //High Speed Oscillator, PLL enabled #pragma config FCMEN = OFF #pragma config IESO = OFF #pragma config PWRT = OFF #pragma config BOR = ON #pragma config BORV = 3 #pragma config VREGEN = ON //USB Voltage Regulator #pragma config WDT = OFF #pragma config WDTPS = 32768 #pragma config MCLRE = ON #pragma config LPT1OSC = OFF #pragma config PBADEN = OFF #pragma config STVREN = ON #pragma config LVP = OFF #pragma config XINST = OFF // Extended Instruction Set #pragma config CP0 = OFF #pragma config CP1 = OFF #pragma config CPB = OFF #pragma config WRT0 = OFF #pragma config WRT1 = OFF #pragma config WRTB = OFF // Boot Block Write Protection #pragma config WRTC = OFF #pragma config EBTR0 = OFF #pragma config EBTR1 = OFF #pragma config EBTRB = OFF // Definitions #define LCD_DD7 LATDbits.LATD7 // LCD data pin 7 #define LCD_DD6 LATDbits.LATD6 // LCD data pin 6 #define LCD_DD5 LATDbits.LATD5 // LCD data pin 5 #define LCD_DD4 LATDbits.LATD4 // LCD data pin 4 #define LCD_DATA LATD #define LCD_RS LATBbits.LATB4 // LCD Register Select SK40C #define LCD_E LATBbits.LATB5 // LCD Enable pin SK40C # define Motor PORTDbits.RD0 # define Fan PORTDbits.RD1 # define Blower PORTDbits.RD2 # define Lamp PORTDbits.RD3 #define sw1 PORTBbits.RB0 // Function Prototypes //------LCD------------ void lcd_init(void); void lcd_4bit_write(unsigned char data); void lcd_config(unsigned char command); void lcd_putchar(unsigned char data); void lcd_putstr(rom const char *str); void lcd_clear(void); void lcd_home(void); void lcd_set_cursor(unsigned char uc_column, unsigned char uc_row); // uc_column is 0 for first character, void lcdprint(unsigned char x,unsigned char *str) ; // uc_row is 0 for 1st line void lcd_goto(unsigned char address); // move cursor with the address according to datasheet of LCD void lcd_e_clock(void); void lcd_send_dec(unsigned long data,unsigned char num_of_digit); //-----DELAY----------- void delay_ms(unsigned int ui_data); // delay in millisecond, maximum value is 65,535; 0 will result in 65536 millisecond of delay void delay_us(unsigned char uc_data); // delay in microsecond, maximum value is 255; 0 will result in 256 microsecond of delay void delay (unsigned long i); //-----PIN INITIALIZATION----------- void init_io ( void ) ; //-----GLOBAL VARIABLE----- unsigned int result; void main(void) { init_io(); lcd_init(); // Initialize LCD lcd_clear(); delay(400); lcdprint(0x00+2,"AUTOMATIC"); lcdprint(0x40+1,"SHOE DRYER"); delay(50000); // lcd_clear(); // delay(200); while(1) //if button SW1 is pressed { if(sw1==0) { lcd_clear(); lcdprint(0x00+3,"PLEASE WAIT"); lcdprint(0x40+1," THANK YOU :)"); delay(50000); // delay(2000); Motor = 1; Lamp = 1; Blower =1; delay(6000000); } else { Motor = 0; Lamp = 0; Blower =1; } } ADCON0 = 0b00000001; // bit5-2 0000 select channel 0 conversion // bit1 A/D conversion status bit // 1- GO to starts the conversion // 0 - DONE when A/D is completed // bit0 Set to 1 to power up A/D ADCON1 = 0b00001100; // bit5 reference is VSS // bit4 reference is VDD // bit3-0 AN2 to AN0 Analog, the rest Digital ADCON2 = 0b00010110; // bit7 : A/D Result Format. 0 Left, 1 Right justified ( Set it to 1, Right Justified) // bit5-3 : 010 acquisition time = 4 TAD // bit2-0 : 110 conversion clock = Tosc / 16 for(;;) { unsigned int t; // variable for temperature ADCON0bits.GO = 1; // This is bit2 of ADCON0, START CONVERSION NOW while(ADCON0bits.GO == 1); // Waiting for DONE t=(ADRESH * 0.48876); //Convert to Degree Celcius - ADRES (16bit) is the output of ADC } } //----------------------- //-----Functions----- //----------------------- // Initialize the I/O port direction. void init_io () { TRISA = 0b00000001; TRISB = 0b11001111; TRISC = 0b11111111; TRISD = 0b00000000; TRISE = 0b00000000; PORTA = 0b00000000; PORTB = 0b00000000; PORTC = 0b11111111; PORTD = 0b00000000; PORTE = 0b00000000; } //-----LCD------- //4-bit mode configuration void lcd_init() { delay_ms(30); // wait for 30ms after power ON for LCD internal controller to initialize itself LCD_E = 1; //Set lcd to configuration mode LCD_RS = 0; // Selected command register delay_us(5); // macro from HITECH compiler to generate code to delay for 1 microsecond base on _XTAL_FREQ value LCD_DATA = (LCD_DATA & 0x0F) | 0b00110000; //make it in 8-bit mode first, for 3 times lcd_e_clock(); delay_ms(2); LCD_DATA = (LCD_DATA & 0x0F) | 0b00110000; //make it in 8-bit mode first, for 3 times lcd_e_clock(); delay_ms(2); LCD_DATA = (LCD_DATA & 0x0F) | 0b00110000; //make it in 8-bit mode first, for 3 times lcd_e_clock(); delay_ms(2); LCD_DATA = (LCD_DATA & 0x0F) | 0b00100000; //make it in 4-bit mode lcd_e_clock(); delay_ms(2); //start sending command in 4 bit mode //Function Set lcd_config(0b00101000); // 0b 0 0 1 ID N F X X // Interface Data Length, ID= 4-bit // Number of line to display, N = 1 is 2 line display, N = 0 is 1 line display // Display Font, F = 0 is 5x 8 dots, F = 1 is 5 x 11 dots //Command Entry Mode lcd_config(0b00000110); // 0b 0 0 0 0 0 1 ID SH // ID = 1, cursor automatic move to right, increase by 1 // SH = 0, shift of entire display is not perform //Display Control lcd_config(0b00001110); // 0b 0 0 0 0 1 D C B // D = 1, Display is ON // C = 0, Cursor is not display // B = 0. Cursor does not blink lcd_clear(); // clear LCD and move the cursor back to home position } //Routine to send data to LCD via 2 nibbles in 4-bit mode void lcd_4bit_write(unsigned char data) { LCD_DATA = (LCD_DATA & 0x0F) | (data & 0xF0); // send out the Most Significant Nibble lcd_e_clock(); LCD_DATA = (LCD_DATA & 0x0F) | (data << 4); // send out the Least Significant Nibble lcd_e_clock(); } //Routine to send command to LCD void lcd_config(unsigned char command) { LCD_RS = 0; //Selected command register delay_us(5); lcd_4bit_write(command); //Send command via 2 nibbles delay_ms(1); } //Routine to send display data (single character) to LCD void lcd_putchar(unsigned char data) { LCD_RS = 1; //Selected data register delay_us(5); lcd_4bit_write(data); //Send display via 2 nibbles delay_ms(1); } //Routine to send string to LCD void lcd_putstr(rom const char *str) { while(*str != '\0') //loop till string ends { lcd_putchar(*str++); //send characters to LCD one by one } } //Routine to clear the LCD void lcd_clear(void) { lcd_config(0x01); //command to clear LCD delay_ms(1); } //function to move LCD cursor to home position void lcd_home(void) { lcd_config(0x02); //command to move cursor to home position delay_ms(1); } //Rountine to set cursor to the desired position base on coordinate, column and row /*Place a string in the specified row and column of the screen. * +--+--+--+--+--+---------------------+ * |0 |1 |2 |3 |4 |5 ...etc | <- row 0 * +--+--+--+--+--+---------------------+ * |0 |1 |2 |3 |4 |5 ...etc | <- row 1 * +--+--+--+--+--+---------------------+ */ void lcd_set_cursor(unsigned char uc_column, unsigned char uc_row) { if(uc_row == 0) lcd_config(0x80 + uc_column); //command to move cursor to first row/line with offset of column else if(uc_row ==1 ) lcd_config(0xC0 + uc_column); //command to move cursor to 2nd row/line with offset of column } //Rountine to set cursor to the desired position base on LCD DDRAM address /*Place a string in the specified row and column of the screen. * +--+--+--+--+--+---------------------+ * |0x00 |0x01 |0x02 |0x03 |0x04 |0x05 ...etc |0x0F| <- 1st line * +--+--+--+--+--+---------------------+ * |0x40 |0x41 |0x42 |0x43 |0x44 |0x45 ...etc |0x4F| <- 2nd line * +--+--+--+--+--+---------------------+ */ void lcd_goto(unsigned char address) { lcd_config(0x80 + address); //command to move cursor to desire position base on the LCD DDRAM address } // function to output enable clock pulse to LCD void lcd_e_clock(void) { delay_us(10); LCD_E = 0; // create a falling edge for Enable pin of LCD to process data delay_us(100); LCD_E = 1; // pull the Enable pin high again delay_us(100); } // Simple delay function // delay in milli second, maximum value for ui_data is 65,535. void delay_ms(unsigned int ui_data) { for(;ui_data > 0; ui_data--) { Delay10KTCYx(1); // to obtain 1 millisecond, we require 12K cycle of instruction if CPU clock is 48MHz Delay1KTCYx(2); // Delay macro from MCC18 Compiler } } // delay in milli second, maximum value for ui_data is 65,535. void delay_us(unsigned char uc_data) { for(;uc_data > 0; uc_data--) { Delay10TCYx(1); // to obtain 1 microsecond delay, we require 12 cycle of instruction if CPU clock is 48MHz } } void delay (unsigned long i) { for (; i>0; i-=1); } void lcd_send_dec(unsigned long data,unsigned char num_of_digit) { if(num_of_digit>=7) { data=data%10000000; lcd_4bit_write(data/1000000+0x30); } if(num_of_digit>=6) { data=data%100000; lcd_4bit_write(data/10000+0x30); } if(num_of_digit>=5) { data=data%100000; lcd_4bit_write(data/10000+0x30); } if(num_of_digit>=4) { data=data%10000; lcd_4bit_write(data/1000+0x30); } if(num_of_digit>=3) { data=data%1000; lcd_4bit_write(data/100+0x30); } if(num_of_digit>=2) { data=data%100; lcd_4bit_write(data/10+0x30); } if(num_of_digit>=1) { data=data%10; lcd_4bit_write(data+0x30); } } void lcdprint(unsigned char x,unsigned char *str) { lcd_goto(x); lcd_putstr(str); }
Last edited by a moderator: