I have started PIC programming for PIC16F72 micro-controller through MPLAB X IDE and XC8 compiler.Below is my code, it is compiled successfully.
#define _XTAL_FREQ 4000000
#include<xc.h>
#pragma config FOSC = RC // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
char pattern[] = {0b10000001,0b11000011,0b11100111,0b11111111,0b11100111,0b11000011,0b10000001};
void write(char tab)
{
char check;
for(int a=0;a<8;a++)
{
check = ((tab >> a) & 1);
if(check)
{
PORTBbits.RB7=1;
PORTBbits.RB6=0;PORTBbits.RB6=1;
}
else
{
PORTBbits.RB7=0;
PORTBbits.RB6=0;PORTBbits.RB6=1;
}
}
}
void main(void) {
TRISB=0x00; //Initialize as output
PORTBbits.RB6=0;
PORTBbits.RB5=0;
PORTBbits.RB5=1;
while(1)
{
for(int i=0;i<7;i++)
{
write(pattern[i]);
__delay_ms(1000);
}
}
return;
}
When I simulated my code in Proteus it shows below error
Processor has been reset by watch dog timer expiring at xxxxx after every 2.3 seconds.
I have searched for this problem with no success. I am unable to resolve the issue
you forget a letter. You have...
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
it should be
#pragma config WDTEN = OFF
Check out page 60 of the datasheet
Try with
__CONFIG(_WDT_OFF & _PWRTE_ON)
Instead of using #pragma config
You can try to use MPLAB X to generate configuration bits for you.
In MPLAB X, click Window -> PIC Memory Views -> Configuration Bits. New window will show up where you can configure your PIC and disable watchdog. If you click button "Generate Source Code to Output" MPLAB will generate source code with proper Configuration Bits for PIC you're using in project. Here's official Microchip tutorial which describes it in detail -http://microchipdeveloper.com/mplabx:view-and-set-configuration-bits
Proteus, the simulation tool, is not officially provided by microchip (chip manufacturer), also sometimes the pirated copy of software creates issues, one thing you can try is double click on the microcontroller in Proteus and change the configuration word to what you actually want. I suggest you to test the code on physical microcontroller.
Related
I recently started in a fresh company where the programer that did the PIC32 programming is now gone and I do need to troubleshoot his uC-Code.
He disliked the Microchip Harmony Configurator/Framework and thus he is only using the Peripheral Library of the Harmony Framework to do coding (That means he used alot of PLIB_*-functions in his code, taken from examples).
For this he extraced the sys_devcon.c and sys_devcon.h files from framework/system/devcon/src and copied that file to the project's source.
Now my Problem:
Below you see the device.cfg. The Pins OSC1 and OSC2 are connected to the primary oscillator (4MHz). We already confirmed that the oscillator is working correctly by testing it with a LeCroy Teledyne (shows 4MHz).
With the Config given by the file the SystemClock (SYSCLK) is derived by the SYSPLL. Since my IDIV is 1, my MULT is 18 and my ODIV is 1, I expect the SYSCLK to run at 72MHz (4/1*18/1=72).
The PBDIV is set to DIV_2 (so PBCLK=SYSCLK/2 -> 36MHz). The REFCLK is expected to run at 6.4MHz.
HOWEVER:
The measured PBCLK shows 18MHz, the measured REFCLK(SYSPLL as F_REFIN, shows 3.2MHz which means the SYSCLK only runs 36MHz (not expected 72MHz). But taken the config into account everthing should run twice as fast. Im unable to locate the problem why the SYSCLK runs at half speed.
I troubleshooted every single PLIB_OSC*(including PLIB_OSC_SLEW*) function inside the code and havent found a single issue (only 5 lines to use some sort of PLIB_OSC* functions). Maybe someone got some ideas/hints to troubleshoot. It's also not clear if the SYSCLK is actually halved or if there is some divider after the SYSPLL (in between REFCLK and PBCLK). Is there a way i can measure the SYSCLK?
uC: PIC32MX174F256B
XC-Version: V2.10
Harmony: V2.05.01 (only PLIB is used)
MPLAB X IDE: v4.20
#ifndef PIC32MX174F256B_CONFIG_HEADER
#define PIC32MX174F256B_CONFIG_HEADER
#ifdef __cplusplus
extern "C" {
#endif
/* PIC32MX174F256B Configuration Bit Settings */
// DEVCFG3
// USERID = No Setting
// Alternate I/O Select for I2C1 (I2C1 uses the SDA1/SCL1 pins)
#pragma config AI2C1 = OFF
// Alternate I/O Select for I2C2 (I2C2 uses the SDA2/SCL2 pins)
#pragma config AI2C2 = OFF
#pragma config PMDL1WAY = OFF // Peripheral Module Enable Configuration
#pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow reconfigurations)
// DEVCFG2
#pragma config FPLLIDIV = DIV_1 // PLL Input Divider (1x Divider)
#pragma config FPLLMUL = MUL_18 // PLL Multiplier (18x Multiplier)
#pragma config FPLLICLK = PLL_POSC // System PLL Input Clock Selection (POSC is input to the System PLL)
#pragma config FPLLODIV = DIV_1 // System PLL Output Clock Divider (PLL Divide by 1)
#pragma config BOREN = ON // Brown-Out Reset (BOR) Enable (Enable BOR)
#pragma config DSBOREN = ON // Deep Sleep BOR Enable (Enable ZPBOR during Deep Sleep Mode)
#pragma config DSWDTPS = DSPS7 // Deep Sleep Watchdog Timer Postscaler (1:2^11)
#pragma config DSWDTOSC = LPRC // Deep Sleep WDT Reference Clock Selection (Select LPRC as
// DSWDT Reference clock)
#pragma config DSWDTEN = OFF // Deep Sleep Watchdog Timer Enable (Disable DSWDT during
// Deep Sleep Mode)
#pragma config FDSEN = ON // Deep Sleep Enable (Enable DSEN bit in DSCON)
// DEVCFG1
#pragma config FNOSC = SPLL // Oscillator Selection Bits (Primary Osc (HS,EC, XT))
#pragma config FSOSCEN = OFF // Secondary Oscillator Enable (Disabled)
#pragma config IESO = OFF // Internal/External Switch Over (Enabled)
#pragma config POSCMOD = XT // Primary Oscillator Configuration (XT osc mode)
#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled)
// value can also be adjusted in module sys_system from libpic32mx174f256b
#pragma config FPBDIV = DIV_2 // Peripheral Clock Divisor
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch
// Disable, FSCM Disabled)
#pragma config WDTPS = PS1 // Watchdog Timer Postscaler (1:1)
#pragma config WDTSPGM = ON // Watchdog Timer Stop During Flash Programming (Watchdog
// Timer stops during Flash programming)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
#pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window Size is 25%)
// DEVCFG0
#pragma config JTAGEN = OFF // JTAG Enable (JTAG Disabled)
#pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2)
#pragma config PWP = OFF // Program Flash Write Protect (Disable)
#pragma config SMCLR = MCLR_NORM // Soft Master Clear Enable (MCLR pin generates a normal system Reset)
#pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF // Code Protect (Protection Disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
I maybe found the issue:
The Device data sheet states:
‘3’ wait states is required for 0-72 MHz operation
However, the sys_devcon.c only defines 0-2 wait states. But not only that, it will actually define no wait states, since #if defined(PLIB_PCACHE_ExistsWaitState) is not defined.
#if defined(PLIB_PCACHE_ExistsWaitState)
if (PLIB_PCACHE_ExistsWaitState(PCACHE_ID_0))
{
int ws; /* number of wait states */
if (sysclk <= 30000000)
ws = 0;
else if (sysclk <= 60000000)
ws = 1;
else
ws = 2;
/* Interrupts must be disabled when changing wait states */
int_flag = (bool)(PLIB_INT_GetStateAndDisable( INT_ID_0 ) & 0x01);
PLIB_PCACHE_WaitStateSet(PCACHE_ID_0, ws);
if (int_flag)
{
PLIB_INT_Enable(INT_ID_0);
int_flag = false;
}
}
#endif // defined(PLIB_PCACHE_ExistsWaitState)
However,
#if defined(PLIB_PCACHE_ExistsWaitState)
is not defined, thus it will never assign wait states.
Do I simply define it with #define PLIB_PCACHE_ExistsWaitState or will I run into an error?
I finally solved the issue by myself.
It was simply my lack of knowledge. I found the crucial information in 2 seperate data sheets.
One was the "PIC32MX1XX/2XX 28/44-PIN XLP FAMILY Data Sheet" on page 318, which states that the maximum SPICLK must not exceed 25MHz (I believe .
The second crucial information was the calculation of the BaudRate.
I thought that the BaudRate equals the selected PBCLK (so when I have a SYSCLK of 72MHz and the PBCLK is SYSCLK=PBCLK/2 => 36MHz, the BaudRate is also set at 36MHz).
But at page 23-29 at "PIC32 FRM Section 23. Serial Peripheral Interface (SPI)" there is the formula
F_SCK = F_PB / ( 2 ⋅ ( SPIxBRG + 1 ))
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.
I am using a PIC16f688 to try to read analog input and to turn a light on or off based on the voltage read. Using this PIC, I have successfully gotten the light to blink using on and off. Here is the code I used for that.
void main() {
ANSEL = 0b00000000; //All I/O pins are configured as digital
CMCON0 = 0x07; // Disbale comparators
TRISC = 0b00000000; // PORTC All Outputs
TRISA = 0b00001000; // PORTA All Outputs, Except RA3
do {
RC0 = 1;
__delay_ms(500);
RC0 = 0;
__delay_ms(500);
} while (1); // Infinite Loop
}
After reading different things I ended up with this code to try to read the analog input.
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#include <xc.h>
#include <pic16f688.h>
#define _XTAL_FREQ 8000000
void main() {
int voltage;
ANSEL = 0b01000000; //All I/O pins are configured as digital except an6/RC2
TRISC = 0b00000100; // PORTC All Outputs except RC2
TRISA = 0b00001000; // PORTA All Outputs, Except RA3
do {
ADCON0 = 0xbb; //set to read
GO_nDONE = 1;
while(GO_nDONE == 1);
voltage = (ADRESH << 8) + ADRESL; //get voltage reading
if(voltage > 500){ //if voltage is greater than 500 out of 1023 turn light on
RC0 = 1;
}
else{
RC0 = 0;
}
__delay_ms(500);
} while (1); // Infinite Loop
}
When I run this, the light is on no matter what the input is, including when the input is wired to ground.
I am using XC8 compiler in MPLab.
I hope that you are using external oscillator otherwise you have set wrong internal oscillator!
Default ADC clock is Fosc/2 and is to high for 8Mhz, so set the ADCON1 to Fosc/16 check datasheet.
Why did you remove this line?
CMCON0 = 0x07; // Disbale comparators
It is essential!
Try disabling the comparators using CMCON0 = 0x07; then RC0 should function normally.
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)