I interfaced RFID with PIC 18f4550, I tried displaying values of RCREG on LCD, but only got a box displayed. What could be the problem?
I am using MPLAB X with the XC8 compiler
// Program to interface RFID module using EUSART in PIC18F2550
#include <p18f4550.h>
#include <stdio.h>
/* _CPUDIV_OSC1_PLL2_1L, // Divide clock by 2
_FOSC_HS_1H, // Select High Speed (HS) oscillator
_WDT_OFF_2H, // Watchdog Timer off
MCLRE_ON_3H // Master Clear on
*/
// CONFIG1L
#pragma config PLLDIV = 1 // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1 // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)
// CONFIG1H
#pragma config FOSC = INTOSC_HS // Oscillator Selection bits (Internal oscillator, HS oscillator used by USB (INTHS))
#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 = ON // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting)
#pragma config VREGEN = OFF // USB Voltage Regulator Enable bit (USB voltage regulator disabled)
// CONFIG2H
#pragma config WDT = ON // Watchdog Timer Enable bit (WDT enabled)
#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 = ON // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#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
// Program to interface RFID module using EUSART in PIC18F4550
// Configuration bits
/* _CPUDIV_OSC1_PLL2_1L, // Divide clock by 2
_FOSC_HS_1H, // Select High Speed (HS) oscillator
_WDT_OFF_2H, // Watchdog Timer off
MCLRE_ON_3H // Master Clear on
*/
#define FREQ 20000000
#define baud 9600
#define spbrg_value 19
#define rs LATAbits.LATA0
#define rw LATAbits.LATA1
#define en LATAbits.LATA2
#define lcdport LATB
unsigned char rx_data();
void lcd_ini();
void lcdcmd(unsigned char);
void lcddata(unsigned char);
unsigned char data[]="Unique ID No.";
unsigned char card_id[12];
unsigned int i=0,j=0,pos;
void Delay_ms(int t)
{
int i;
int j;
for (i=0;i<t;i++)
for(j=0;j<100;j++);
}
void main()
{
TRISB=0; // Set Port B as output port
LATB=0;
TRISA=0;
LATA=0;
SPBRG=spbrg_value; // Fill SPBRG register to set the baud rate
RCSTAbits.SPEN=1; // To activate serial port (Tx and Rx pins)
RCSTAbits.CREN=1; // To enable continuous reception
PIE1bits.RCIE=1; // To enable the Reception (Rx) Interrupt
INTCONbits.GIE=1;
INTCONbits.PEIE=1;
lcd_ini(); // LCD initialization
while(data[i]!='\0')
{
//lcddata(data[i]); // To send characters one by one from 'data' array
i++;
}
while(1)
{
i=0;
while(PIR1bits.RCIF ==0);
}
}
void interrupt ISR()
{
i=0;
unsigned char c = 'P';
lcddata(c);
while(i<12)
{
card_id[i]=RCREG;
lcddata(card_id[i]);
i++;
}
card_id[11]='\0';
/*while(i<12)
{
char cc=card_id[i];
lcddata(cc);
i++;// Print the 12 byte received
}}
while(data[i]!='\0'){
lcddata(data[i]);
i++;
}*/
}
void lcd_ini()
{
lcdcmd(0x38); // Configure the LCD in 8-bit mode, 2 line and 5x7 font
lcdcmd(0x0C); // Display On and Cursor Off
lcdcmd(0x01); // Clear display screen
lcdcmd(0x06); // Increment cursor
lcdcmd(0x80); // Set cursor position to 1st line, 1st column
}
void lcdcmd(unsigned char cmdout)
{
lcdport=cmdout; //Send command to lcdport=PORTB
rs=0;
rw=0;
en=1;
Delay_ms(10);
en=0;
}
void lcddata(unsigned char dataout)
{
lcdport=dataout; //Send data to lcdport=PORTB
rs=1;
rw=0;
en=1;
Delay_ms(10);
en=0;
}
I keep getting an error saying illegal conversion of pointer to string on the line where i assign card_id[i] the value of RCREG and it seems that RCREG is returning the address
The watchdog timer is enabled #pragma config WDT = ON but never cleared. Periodically call ClrWdt(); or disable it, otherwise the program will reset every time the watchdog timer expires.
Not causing a problem but worth mentioning, with XC8 Microchip recommends using #include <xc.h> which will make including the processor specific header unnecessary.
The character that is in RCREG is being moved into all 12 elements of card_id, was that really intended?
The UART will assert an interrupt and set RCIF each time a character is received. Reading RCREG clears the flag. It's generally not a good idea to do tasks (especially ones with delays) while servicing the interrupt because incoming characters can be missed. Consider establishing a circular queue and doing something like this:
void interrupt ISR()
{
while(PIR1bits.RC1IF){
queue1[q1tail] = RCREG1;
if(++q1tail >= Q_SIZE)
q1tail = 0;
}
}
Then in the main forever loop check the queue. When something comes in (q1tail != q1head) process it as necessary and pop it off the queue (increment q1head).
It's also a good idea to check for overrun and framing errors. It can be done when checking the queue pointers:
if (RCSTA1bits.OERR || RCSTA1bits.FERR)
{
RCSTA1bits.CREN = 0;
Nop();
Nop();
RCSTA1bits.CREN = 1;
}
Related
I can't initialize a QAPASS 1602A LCD with a PIC18F4620, I always get square blocks on both lines. In the beginning, I thought the LCD was broken so I got a new one but the outcome is the same. I use a logifind PIC 40 Mini development board connected to PC through a PICkit3, everything else is working fine on every port but not the LCD.
Thanks a lot for your help.
//main.c
#include "newxc8_header.h"
#include <xc.h>
#include "lcd_16x2.h"
void main(void) {
ADCON1=0x0F; //configuring all analog ports to digital
TRISB=0x00; //Set RBs as output
TRISD=0x00; //Set RDs as output
TRISE=0x00; //Set REs as output
LCD_Init(); //Initialize 16x2 LCD
LCD_String("Hello");
while(1);
}
//lcd_16x2.c
#include "lcd_16x2.h"
void LCD_Init(void)
{
__delay_ms(1000);
EN = 0;
RS = 0;
ldata = 0x00;
LCD_Command(0x38); // Initialization of 16X2 LCD: 8-bit, 2 Lines, 5x7 Dots
LCD_Command(0x0C); // Display ON Cursor OFF
LCD_Command(0x01); //clear LCD
LCD_Command(0x02); //cursor beginning first line
}
void LCD_Command(unsigned char cmd)
{
ldata = cmd; /* Send data to PORT as a command for LCD */
RS = 0; /* Command Register is selected */
EN = 1; /* High-to-Low pulse on Enable pin to latch data */
__delay_ms(5);
EN = 0;
__delay_ms(3);
}
void LCD_Char(unsigned char data)
{
ldata = data; /* Send data to LCD */
RS = 1; // Data Register is selected */
EN = 1; // High-to-Low pulse on Enable pin to latch data */
__delay_ms(5);
EN = 0;
__delay_ms(3);
}
void LCD_String(const char *msg)
{
while(*msg != 0)
{
LCD_Char(*msg);
msg++;
}
}
//lcd_16x2.h
#ifndef LCD_16X2_H
#define LCD_16X2_H
#ifdef __cplusplus
extern "C" {
#endif
#include <xc.h>
#define _XTAL_FREQ 16000000 //16 MHz (4 MHz x 4 PLL)
#define ldata LATB /*PORTB(RB0-RB7) is used for transmitting data to LCD*/
#define RS LATEbits.LATE0 /*RE0 pin is used for Register Select*/
#define EN LATEbits.LATE1 /*RE1 pin is used for Enable*/
void LCD_Init(void);
void LCD_Command(unsigned char );
void LCD_Char(unsigned char x);
void LCD_String(const char *);
void LCD_Clear();
#ifdef __cplusplus
}
#endif
#endif /* NEWFILE_H */
Your question does not contain enough details for a definitive answer.
The best guess is that you have a wiring error between your LogiFind PIC-40-MINI board:
and your QAPASS 1602A LCD module:
A photograph of how they are connected would be helpful.
If you are convinced the wiring is correct then a problem may be in the LCD_Init() function, see HD44780 data sheet page 45 for a flowchart about initializing the module in 8-bit parallel mode or in the code you have not posted.
Added on January 19, 2020:
I assembled a PIC18F4620 and LCD module as you described.
Found that your code as posted does not show the configuration words you are using.
When using the internal oscillator at the default frequency (1MHz) your code runs but it's 16 times slower than expected.
After adjusting some settings this is the final version of code:
//main.c
// PIC18F4620 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1H
#pragma config OSC = INTIO67 // Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7)
#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 BOREN = 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)
// 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 = PORTC // 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 Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP 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-003FFFh) not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (004000-007FFFh) not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (008000-00BFFFh) not code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (00C000-00FFFFh) not code-protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-003FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (004000-007FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (008000-00BFFFh) not write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (00C000-00FFFFh) not write-protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)
#include <xc.h>
#include "lcd_16x2.h"
void main(void) {
OSCCON = 0b01100000; // sets clock = 4MHz
OSCTUNE = 0b01000000; // turn on 4xPLL
ADCON1=0x0F; //configuring all analog ports to digital
TRISB=0x00; //Set RBs as output
TRISD=0x00; //Set RDs as output
TRISE=0x00; //Set REs as output
LCD_Init(); //Initialize 16x2 LCD
LCD_Command(0x80);
LCD_String("Hello");
LCD_Command(0xC0);
LCD_String(__TIME__ " v1 ");
while(1);
}
//lcd_16x2.c
#include <xc.h>
#include "lcd_16x2.h"
void LCD_Init(void)
{
__delay_ms(1000);
EN = 0;
RS = 0;
LCD_Command(0x38); // Initialization of 16X2 LCD: 8-bit, 2 Lines, 5x7 Dots
LCD_Command(0x0C); // Display ON Cursor OFF
LCD_Command(0x01); //clear LCD
LCD_Command(0x02); //cursor beginning first line
}
void LCD_Command(unsigned char cmd)
{
ldata = cmd; /* Send data to PORT as a command for LCD */
RS = 0; /* Command Register is selected */
EN = 1; /* High-to-Low pulse on Enable pin to latch data */
__delay_us(1);
EN = 0;
__delay_ms(4);
}
void LCD_Char(unsigned char data)
{
ldata = data; /* Send data to LCD */
RS = 1; // Data Register is selected */
EN = 1; // High-to-Low pulse on Enable pin to latch data */
__delay_us(1);
EN = 0;
__delay_us(50);
}
void LCD_String(const char *msg)
{
while(*msg != 0)
{
LCD_Char(*msg);
msg++;
}
}
//lcd_16x2.h
#ifndef LCD_16X2_H
#define LCD_16X2_H
#ifdef __cplusplus
extern "C" {
#endif
#include <xc.h>
#define _XTAL_FREQ 16000000 //16 MHz (4 MHz x 4 PLL)
#define ldata LATB /*PORTB(RB0-RB7) is used for transmitting data to LCD*/
#define RS LATEbits.LATE0 /*RE0 pin is used for Register Select*/
#define EN LATEbits.LATE1 /*RE1 pin is used for Enable*/
void LCD_Init(void);
void LCD_Command(unsigned char );
void LCD_Char(unsigned char x);
void LCD_String(const char *);
void LCD_Clear();
#ifdef __cplusplus
}
#endif
#endif /* NEWFILE_H */
Strange question.
I have two PICDEM FS USB Demo Boards from Microchip (with the PIC18F45K50 chip on them). I ordered a second one because the first was acting really strange and I figured it was defective. But the second one does it too.
The behavior is that it seems like the clock on the chip only runs when I touch the OSC1 pin as though my body is somehow acting as the ground for the chip. I have a male-to-male header wire stuck in the OSC1 header port and when I pinch it, the LEDs blink as though the system clock is running.
If I take my fingers off, it's like the system clock stops. I have a few different programs that flash based on time, pressing the buttons, or turning the potentiometer. But they all have the same behavior: unless I pinch the wire, nothing happens, like the system clock is stopped. Note that connecting the OSC1 pin to the GND pin does accomplish the same thing, so my hypothesis about my body acting as the ground doesn't seem to make much sense.
I can't find anything about this in the user guide for the board, or any help threads mentioning the same behavior.
My only guess is that maybe it's something about the configuration flags I'm using in my programs, but from what I understand they look fine:
// PIC18F45K50 Configuration Bit Settings
// CONFIG1L
#pragma config PLLSEL = PLL4X // PLL Selection (4x clock multiplier)
#pragma config CFGPLLEN = OFF // PLL Enable Configuration bit (PLL Disabled (firmware controlled))
#pragma config CPUDIV = NOCLKDIV// CPU System Clock Postscaler (CPU uses system clock (no divide))
#pragma config LS48MHZ = SYS24X4// Low Speed USB mode with 48 MHz system clock (System clock at 24 MHz, USB clock divider is set to 4)
// CONFIG1H
#pragma config FOSC = HSH // Oscillator Selection (HS oscillator, high power 16MHz to 25MHz)
#pragma config PCLKEN = ON // Primary Oscillator Shutdown (Primary oscillator enabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover (Oscillator Switchover mode disabled)
// CONFIG2L
#pragma config nPWRTEN = ON // Power-up Timer Enable (Power up timer enabled)
#pragma config BOREN = SBORDIS // Brown-out Reset Enable (BOR enabled in hardware (SBOREN is ignored))
#pragma config BORV = 190 // Brown-out Reset Voltage (BOR set to 1.9V nominal)
#pragma config nLPBOR = OFF // Low-Power Brown-out Reset (Low-Power Brown-out Reset disabled)
// CONFIG2H
#pragma config WDTEN = OFF // Watchdog Timer Enable bits (WDT disabled in hardware (SWDTEN ignored))
#pragma config WDTPS = 32768 // Watchdog Timer Postscaler (1:32768)
// CONFIG3H
#pragma config CCP2MX = RC1 // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = OFF // PORTB A/D Enable bit (PORTB<5:0> pins are configured as digital I/O on Reset)
#pragma config T3CMX = RC0 // Timer3 Clock Input MUX bit (T3CKI function is on RC0)
#pragma config SDOMX = RB3 // SDO Output MUX bit (SDO function is on RB3)
#pragma config MCLRE = ON // Master Clear Reset Pin Enable (MCLR pin enabled; RE3 input disabled)
// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset (Stack full/underflow will cause Reset)
#pragma config LVP = ON // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled if MCLRE is also 1)
#pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming Port Enable (ICPORT disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled)
// CONFIG5L
#pragma config CP0 = OFF // Block 0 Code Protect (Block 0 is not code-protected)
#pragma config CP1 = OFF // Block 1 Code Protect (Block 1 is not code-protected)
#pragma config CP2 = OFF // Block 2 Code Protect (Block 2 is not code-protected)
#pragma config CP3 = OFF // Block 3 Code Protect (Block 3 is not code-protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protect (Boot block is not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protect (Data EEPROM is not code-protected)
// CONFIG6L
#pragma config WRT0 = OFF // Block 0 Write Protect (Block 0 (0800-1FFFh) is not write-protected)
#pragma config WRT1 = OFF // Block 1 Write Protect (Block 1 (2000-3FFFh) is not write-protected)
#pragma config WRT2 = OFF // Block 2 Write Protect (Block 2 (04000-5FFFh) is not write-protected)
#pragma config WRT3 = OFF // Block 3 Write Protect (Block 3 (06000-7FFFh) is not write-protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Registers Write Protect (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protect (Boot block (0000-7FFh) is not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protect (Data EEPROM is not write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Block 0 Table Read Protect (Block 0 is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Block 1 Table Read Protect (Block 1 is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Block 2 Table Read Protect (Block 2 is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Block 3 Table Read Protect (Block 3 is not protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protect (Boot block is not protected from table reads executed in other blocks)
Any help would be appreciated.
It turned out to be just as I suspected, one of the configuration flags was wrong. Changing the setting of FOSC fixed the problem. The new line is:
#pragma config FOSC = INTOSCIO // Oscillator Selection (Internal oscillator)
Being new to PIC programming I'm not sure what the external oscillator was that it was expecting but somehow it ended up being me.
If it was working at all the board probably has an external oscillator on it and you must have selected the wrong gain setting for it. That is the same setting but if you select eg XT for an oscillator which needs HS then it will be marginal and start working only when you touch it and in doing so change the load capacitance.
I'm having problems with timer1 external clock interrupts.
Basically I'm using internal 4mhz clock and a 32khz crystal generator connected to T1OSI/T1OSO pins.
The thing is that I'm unable to generate interrupt from external clock. Below is the code I'm trying to run:
#define _XTAL_FREQ 4000000
// CONFIG
#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#include <xc.h>
void interrupt isr(void){
if(TMR1IF){
RA0=!RA0;
}
}
void main(void) {
CMCON = 0x07; // comparators off
TRISA = 0x00;
RA0=1;
__delay_ms(1000);
RA0=0;
TMR1CS=1;
T1CONbits.nT1SYNC = 0
TMR1IF = 0; // Clear the timer1 interrupt flag
TMR1H = 224;
TMR1L = 0;
TMR1IE = 1;
TMR1ON = 1;
T1CKPS1 = 1;
INTCON = 0b11000000;
while(1);
}
This code works if using internal clock, but if configured to use external - it fails.
Maybe there's something I did wrong? Thank you for any ideas.
Okay, after some more tries, I've managed to got it working.
Using 33pf capacitors on 32khz external crystal.
Code:
#define _XTAL_FREQ 4000000
// CONFIG
#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#include <xc.h>
void interrupt isr(void){
if(TMR1IF){
TMR1ON = 0;
RA0=~RA0;
TMR1H = 0x80;
TMR1L = 0x00;
TMR1IF = 0;
TMR1ON = 1;
}
}
void main(void) {
CMCON = 0x07; // comparators off
TRISA = 0x00;
RA0=0;
__delay_ms(5000);
T1CKPS0 = 0;
T1CKPS1 = 0;
TMR1CS=1;
TMR1H = 0x80;
TMR1L = 0x00;
T1OSCEN = 1;
T1CONbits.nT1SYNC = 1;
TMR1IE = 1;
GIE=1;
PEIE=1;
TMR1ON = 1;
while(1);
}
Now getting correct 1s pulses on RA0.
Basically it was badly configured pre-scaler and counter register values.
Also you need to use TMR1ON after configuring timer.
This is a simple program to on/off led in XC8 (Microchip):
1) This code work :
#include <xc.h>
#define _XTAL_FREQ 4000000
#pragma config FOSC = HS // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Disable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = ON // Brown-out Detect Enable bit (BOD enabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
void main()
{
TRISB0=1;
TRISB4=0;
if (RB0==1)
{
RB4 = 1;
}
else
{
RB4 = 0;
}
}
Switch Connected to portb RB0
2) This code doesn't work :
#include <xc.h> // LibrerÃa XC8
#define _XTAL_FREQ 4000000 // Indicamos a que frecuencia de reloj esta funcionando el micro
// PIC16F648A Configuration Bit Settings
#pragma config FOSC = HS // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = ON // Brown-out Detect Enable bit (BOD enabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
void main()
{
TRISA0=1;
TRISB4=0;
if (RA0==1)
{
RB4 = 1;
}
else
{
RB4 = 0;
}
}
Switch Connected to port RA0
Why I can on led If I get port B but not in port A ?
Best Regards.
The ANSEL should be set correctly or the ADC might override it and make it an input as commented on above.
But to set PORT pin outputs you generally
use the LATCH register. RB4 is what you read when the pin is configured as a digital input. LATB4 is the output latch.
LATB4 = X;
where X is 1 or 0
I'm trying to debugging my code on the PIC24F04KA200. But when i start debugging, i get the message:
The target device is not ready for debugging. Please check your configuration bit settings and program the device before proceeding. The most common causes for this failure are oscillator and/or PGC/PGD settings.
I readed the PicKit3 User guide, but i don't find the answer. The PicKit is connected to the PGC2 and PGD2 pins. My configuration bits:
/*
* File: config.h
*
*/
#ifndef CONFIG_H
#define CONFIG_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif /* CONFIG_H */
#include <p24F04KA200.h>
// PIC24F04KA200 Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// FBS
// FGS
#pragma config GWRP = OFF // General Segment Code Flash Write Protection bit (General segment may be written)
#pragma config GCP = OFF // General Segment Code Flash Code Protection bit (No protection)
// FOSCSEL
#pragma config FNOSC = FRCDIV // Oscillator Select (8 MHz FRC oscillator with divide-by-N (FRCDIV))
#pragma config IESO = OFF // Internal External Switch Over bit (Internal External Switchover mode disabled (Two-Speed Start-up disabled))
// FOSC
#pragma config POSCMOD = NONE // Primary Oscillator Configuration bits (Primary oscillator disabled)
#pragma config OSCIOFNC = ON // CLKO Enable Configuration bit (CLKO output disabled)
#pragma config POSCFREQ = MS // Primary Oscillator Frequency Range Configuration bits (Primary oscillator/external clock input frequency between 100 kHz and 8 MHz)
#pragma config SOSCSEL = SOSCHP // SOSC Power Selection Configuration bits (Secondary oscillator configured for high-power operation)
#pragma config FCKSM = CSECMD // Clock Switching and Monitor Selection (Clock switching is enabled, Fail-Safe Clock Monitor is disabled)
// FWDT
#pragma config WDTPS = PS32768 // Watchdog Timer Postscale Select bits (1:32,768)
#pragma config FWPSA = PR128 // WDT Prescaler (WDT prescaler ratio of 1:128)
#pragma config WINDIS = OFF // Windowed Watchdog Timer Disable bit (Standard WDT selected; windowed WDT disabled)
#pragma config FWDTEN = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
// FPOR
#pragma config BOREN = BOR0 // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware; SBOREN bit disabled)
#pragma config PWRTEN = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BORV = LPBOR // Brown-out Reset Voltage bits (Low-Power Brown-out reset occurs around 2.0V)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RA5 input pin disabled)
// FICD
#pragma config ICS = PGx2 // Reserved (PGC2/PGD2 are used for programming the device)
// FDS
#pragma config DSWDTPS = DSWDTPSF // Deep Sleep Watchdog Timer Postscale Select bits (1:2,147,483,648 (25.7 Days))
#pragma config DSLPBOR = OFF // Deep Sleep Zero-Power BOR Enable bit (Deep Sleep BOR disabled in Deep Sleep)
#pragma config DSWDTEN = OFF // Deep Sleep Watchdog Timer Enable bit (DSWDT disabled)
And my main file:
/*
* File: main.c
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
while(1)
{
;
}
}
The processor is running. I have a blinkerLed program running.
Does anyone have a suggestion?
That micro-controller does not have embedded debugging capabilities. If you want to debug your application for this UC, you'll have to use an emulator or the simulator (not my favorite but it can sometimes get you out of a bind).
From the Microchip website:
Featuring nanoWatt XLP Technology ideal for battery applications
(Please use PIC24F16KA102 Processor Extention Pack for Emulation and
Debug Support-AC244028. This MCU does not have on-chip debug
capability)