Hello,
my setup is XC8 2.10 + MPLAB X IDE 5.20
Pic18f4550 with 20 MHz, LCD 4x20 (2004A)
my project works until i add LCD code. compiler dont show any errors, but when i upload hex file to chip, nothing works.
if i comment this line: LCD_Print("MPLAB XC8"); the chip works again (led blinks).
i tried several LCD libraries, but its always the similar problem. im sure my conections between LCD and pic18f4550 are ok:
RS - RB0
E - RB2
Data4 - RB3
Data5 - RB5
Data6 - RB6
Data7 - RB7
attaching my code and LCD library.
thanks a lot.
my setup is XC8 2.10 + MPLAB X IDE 5.20
Pic18f4550 with 20 MHz, LCD 4x20 (2004A)
my project works until i add LCD code. compiler dont show any errors, but when i upload hex file to chip, nothing works.
if i comment this line: LCD_Print("MPLAB XC8"); the chip works again (led blinks).
i tried several LCD libraries, but its always the similar problem. im sure my conections between LCD and pic18f4550 are ok:
RS - RB0
E - RB2
Data4 - RB3
Data5 - RB5
Data6 - RB6
Data7 - RB7
attaching my code and LCD library.
thanks a lot.
Code:
#define _XTAL_FREQ 20000000
// CONFIG1L
#pragma config PLLDIV = 5 // PLL Prescaler Selection bits (Divide by 5 (20 MHz oscillator input))
#pragma config CPUDIV = OSC2_PLL3// System Clock Postscaler Selection bits ([Primary Oscillator Src: /2][96 MHz PLL Src: /3])
#pragma config USBDIV = 2 // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes from the 96 MHz PLL divided by 2)
// CONFIG1H
#pragma config FOSC = HSPLL_HS // Oscillator Selection bits (HS oscillator, PLL enabled (HSPLL))
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting 2.05V)
#pragma config VREGEN = OFF // USB Voltage Regulator Enable bit (USB voltage regulator disabled)
// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
// CONFIG3H
#pragma config CCP2MX = ON // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = OFF // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = OFF // MCLR Pin Enable bit (RE3 input pin enabled; MCLR pin disabled)
// CONFIG4L
#pragma config STVREN = OFF // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
//LCD module connections
#define LCD_RS LATBbits.LATB0
#define LCD_EN LATBbits.LATB2
#define LCD_D4 LATBbits.LATB3
#define LCD_D5 LATBbits.LATB5
#define LCD_D6 LATBbits.LATB6
#define LCD_D7 LATBbits.LATB7
#define LCD_RS_DIR TRISBbits.TRISB0
#define LCD_EN_DIR TRISBbits.TRISB2
#define LCD_D4_DIR TRISBbits.TRISB3
#define LCD_D5_DIR TRISBbits.TRISB5
#define LCD_D6_DIR TRISBbits.TRISB6
#define LCD_D7_DIR TRISBbits.TRISB7
//End LCD module connections
#include <xc.h>
#include "LCD_Lib.c"
void InitBaseFQ(void);
void InitDCO(int dco);
void SetDCO_FQ(int dco,unsigned int fdivider);
int main(void)
{
unsigned int DCO1_FQ = 150;
unsigned int DCO2_FQ = 150;
unsigned int DCO3_FQ = 500;
unsigned int DCO4_FQ = 600;
unsigned int fdivider2;
unsigned int nata = 69;
float x = 2,y,vir;
// RA0 - RA4 pins as OUTPUTS - TIMERS CONTROL
TRISA = 0x00;
// RD0-7 - DATA LINE FOR DIVIDERS - OUTPUT
TRISD = 0x00;
TRISC1 = 1; // button up input
TRISC2 = 1; // button down input
LATAbits.LATA2 = 1; // Starting as 74HC139 not active
LATAbits.LATA5 = 1; // led on
LCD_Begin(); // initialize LCD module
LCD_Goto(1, 1); // go to column 4, row 1
LCD_Print("MPLAB XC8");
InitBaseFQ();
InitDCO(1);
InitDCO(2);
SetDCO_FQ(1,DCO1_FQ);
SetDCO_FQ(2,DCO2_FQ);
while(1) {
LATAbits.LATA5 = 0;
if (PORTCbits.RC1==0) {
DCO2_FQ = DCO2_FQ - 1;
SetDCO_FQ(1,DCO2_FQ);
SetDCO_FQ(2,DCO2_FQ+2);
__delay_ms(600);
}
if (PORTCbits.RC2==0) {
DCO2_FQ = DCO2_FQ + 1;
SetDCO_FQ(1,DCO2_FQ);
SetDCO_FQ(2,DCO2_FQ+2);
__delay_ms(600);
}
__delay_ms(300);
LATAbits.LATA5 = 1;
__delay_ms(300);
}
}
// FUNCTIONS
void InitBaseFQ(void) {
LATAbits.LATA2 = 1; // 74HC139 not active
// Select FIRST (1) 82C54 timer
LATAbits.LATA3 = 1;
LATAbits.LATA4 = 1;
// FIRST (1) 82C54 timer CONTROL WORDS (A0=1,A1=1)
LATAbits.LATA0 = 1; LATAbits.LATA1 = 1;
// CW: Counter2, Read/Write LSB only., Mode3, Binary Counter 16-bit
LATD = 0x96;
LATAbits.LATA2 = 0; // 74HC139 active
LATAbits.LATA2 = 1; // 74HC139 not active
// FIRST (1) 82C54 timer SECTION 0 (A0=0,A1=0)
LATAbits.LATA0 = 0;
LATAbits.LATA1 = 1;
// Daliklis 2 tik LSB
LATD = 2;
LATAbits.LATA2 = 0; // 74HC139 active
LATAbits.LATA2 = 1; // 74HC139 not active
}
void InitDCO(int dco) {
LATAbits.LATA2 = 1; // 74HC139 not active
// Select TIMER
if ((dco==1)||(dco==2)) { LATAbits.LATA3 = 1; LATAbits.LATA4 = 1; }
if ((dco==3)||(dco==4)) { LATAbits.LATA3 = 0; LATAbits.LATA4 = 1; }
if ((dco==5)||(dco==6)) { LATAbits.LATA3 = 1; LATAbits.LATA4 = 0; }
if ((dco==7)||(dco==8)) { LATAbits.LATA3 = 0; LATAbits.LATA4 = 0; }
// CONTROL WORDS (A0=1,A1=1)
LATAbits.LATA0 = 1; LATAbits.LATA1 = 1;
// CONTROL WORD
if ((dco==1)||(dco==3)||(dco==5)||(dco==7)) { LATD = 0x76; }
if ((dco==2)||(dco==4)||(dco==6)||(dco==8)) { LATD = 0x36; }
LATAbits.LATA2 = 0; // 74HC139 active
LATAbits.LATA2 = 1; // 74HC139 not active
}
void SetDCO_FQ(int dco,unsigned int fdivider) {
LATAbits.LATA2 = 1; // 74HC139 not active
if ((dco==1)||(dco==2)) { LATAbits.LATA3 = 1; LATAbits.LATA4 = 1; }
if ((dco==3)||(dco==4)) { LATAbits.LATA3 = 0; LATAbits.LATA4 = 1; }
if ((dco==5)||(dco==6)) { LATAbits.LATA3 = 1; LATAbits.LATA4 = 0; }
if ((dco==7)||(dco==8)) { LATAbits.LATA3 = 0; LATAbits.LATA4 = 0; }
// DCO SELECT (A0,A1)
if ((dco==1)||(dco==3)||(dco==5)||(dco==7)) { LATAbits.LATA0 = 1; LATAbits.LATA1 = 0; }
if ((dco==2)||(dco==4)||(dco==6)||(dco==8)) { LATAbits.LATA0 = 0; LATAbits.LATA1 = 0; }
fdivider = 2000000 / fdivider;
LATD = fdivider & 0xFF;
LATAbits.LATA2 = 0; // 74HC139 active
LATAbits.LATA2 = 1; // 74HC139 not active
LATD = fdivider / 256u;
LATAbits.LATA2 = 0; // 74HC139 active
LATAbits.LATA2 = 1; // 74HC139 not active
}
Attachments
-
3.1 KB Views: 12