I am totally astonished that the prioritized unprogramed, reset pin assignment of most of the pins on the PIC18F25K22 are not I/O.
I seem to be playing musical chairs trying to accomplish what used to a simple task, setting all the PORTA, PORTB, PORTC pins to I/O.
Someone must have found a simple way to do this with XC8 or Hi-Tech C-code. I seem stuck again.
I have attached LEDs and 1k resistors to Vss on each PORTB pin except INT0, bbit0, which is for INT0, it has a momentary Sw to ground a pullup to Vdd.
For example, bbit3 has five other pin use priorities above it, at the bottom of the list is digital I/O!
I have RTFM, but I cannot quite seem to understand what I doing wrong.
For instance, I can get some of PORTB's pins to revert from their unprogrammed PIN PRIORITY to I/O, but not all.
bbit6, bbit1 seem to be out of control and under the influence of some other MCU internal peripheral function.
//------------------------------------------
/*
NOTE: TMR1 is working correctly and blinking the LEDs on bbit2 is doing what it is supposed to, bbit0 INT0 is doing its IRQ ok.
bbit6 flashes properly, bbit7 is erratic, bbit3 is out to lunch, bbit1 is controlled by evil zombies.
I seem to have tried without success to initialize all pins of PORTA, PORTB, PORTC, to be just I/O pins set for only input-output mode.
In this program, every PORTB pin seems to blink properly except for PORTB.1 and PORTB.3 and PORTB.6.
PORTB.0 INT0IE is working as INT0 , PORTB.1 lights on reset, turns off within a second, but then doesn't light again.
PORTB.7 will turn off a visible fraction of a second after it and PORTB.6 are both turned off by code?
So, obviously some mysterious MCU operation has hijacked control of bbit1 an bbit6!
*/
//----------------------
// Target: PIC18F45K22 Hi-Tech 9.80 or MPLABX v3.00 Compiler: XC8 v1.30
// #include <xc.h> //only for XC compiler
// I am using Hi-Tech and XC8, has been compiled on both XC8 1.34 and Hi-Tech P18 ver 9.80 , get exactly the same results
// PIC configuration words
#include <htc.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <P18f25k22.h>
#include <stdint.h>
__PROG_CONFIG(1,0x8800); //Internal Osc Block at 16MHz,IES0 enabled-
//#pragma config FOSC = INTIO67 wks with XC not 9.80
#pragma config FCMEN = OFF
#pragma config IESO = ON //originally set to off
#pragma config BOREN = OFF
#pragma config WDTEN = OFF
#pragma config WDTPS = 1024
#pragma config PBADEN = OFF //PORTB is I/O at reset not A2D
#pragma config HFOFST = ON
#pragma config LVP = OFF //bit2 of CONFIG
#pragma config STVREN = ON
#pragma config XINST = OFF
#pragma config PWRTEN = OFF
#pragma config BORV = 190
#pragma config MCLRE = EXTMCLR
#pragma config CCP2MX = PORTC1
#pragma config CCP3MX = PORTB5
#pragma config T3CMX = PORTC0
#pragma config P2BMX = PORTD2
#pragma config PLLCFG = OFF
#pragma config PRICLKEN = OFF
//Bit 7 6 5 4 <3:0>
// IESO FCMEM PRICLKEN PLLCFG OSC SELECT
// Int-Ext FailSafe no effect 0010 Xtal
// ClkSwEn Clock Mon HFINTOSC 1000 INTHFOSC
// 1=En 0=off 0=Dis x 1000
//OSCCON SCS=0 else no 64MHz PLL operation
/*
__PROG_CONFIG(1,0x8800); //Internal Osc Block at 16MHz,IES0 enabled-
__PROG_CONFIG(2,0x0); //BOREN and WDTEN both disabled
__PROG_CONFIG(3,0xBD00); //PBADEN disabled PORTB<5:0>are I/O not A2d at reset
__PROG_CONFIG(4,0x81); //bit2 =0 LVP disabled, default for all =0x85
*/
__PROG_CONFIG(5,0xC00F); //CONFIG5 to CONFIG7 just set Read and Write protects
__PROG_CONFIG(6,0xE00F); //All mem reads and writes are enabled, no protect
__PROG_CONFIG(7,0x400F); //Default BulkErase settings for CONFIG5--CONFIG7
#pragma config IESO=1; //Needs to be=1 for SCS to work to set clk source.
#pragma config PBADEN=0;
#pragma config WDTEN=0; //WatchDog Enable Disabled 0003=0x3F ater erase
#define Off 0
#define OFF 0
#define off 0
#define On 1
#define ON 1
#define on 1
#define None 0
//Break the ports down to their bits
#define PB(port,bit) ((unsigned) & (port)*8 + (bit))
static bit abit0 @ PB(PORTA,0);
static bit abit1 @ PB(PORTA,1);
static bit abit2 @ PB(PORTA,2);
static bit abit3 @ PB(PORTA,3);
static bit abit4 @ PB(PORTA,4);
static bit abit5 @ PB(PORTA,5);
static bit abit6 @ PB(PORTA,6);
static bit abit7 @ PB(PORTA,7);
static bit bbit0 @ PB(PORTB,0);
static bit bbit1 @ PB(PORTB,1);
static bit bbit2 @ PB(PORTB,2);
static bit bbit3 @ PB(PORTB,3);
static bit bbit4 @ PB(PORTB,4);
static bit bbit5 @ PB(PORTB,5);
static bit bbit6 @ PB(PORTB,6);
static bit bbit7 @ PB(PORTB,7);
static bit cbit0 @ PB(PORTC,0);
static bit cbit1 @ PB(PORTC,1);
static bit cbit2 @ PB(PORTC,2);
static bit cbit3 @ PB(PORTC,3);
static bit cbit4 @ PB(PORTC,4);
static bit cbit5 @ PB(PORTC,5);
static bit cbit6 @ PB(PORTC,6);
static bit cbit7 @ PB(PORTC,7);
static double ttemp=0;
void interrupt high_priority(void)
{
if (INT0IF)
{ INT0IF = 0;
bbit3=1;
bbit4=0;
}
if (TMR1IF) //at 64MHz approx 18.5mSec with H=0x80 L=0
{ TMR1ON = 0;
TMR1IF = 0;
TMR1H = 0x80;
TMR1L = 0x0;
TMR1ON = 1;
if(bbit2==0)
{ bbit2=1;
}
else bbit2=0;
}
}
void interrupt low_priority lp(void)
{
INT0IF=0;
bbit3=0;
bbit4=1;
if (TMR1IF) //at 64MHz takes 37mSec for rollover with H=0,L=0
{
TMR1ON = 0;
TMR1IF = 0;
TMR1H = 0x0;
TMR1L = 0x0;
TMR1ON = 1;
if(bbit2==0)
{ bbit2=1;
}
else bbit2=0;
}
}// end of Low priority interrupt
#define _XTAL_FREQ 64000000L
#define FCYC (_XTAL_FREQ/4L)
void Init_TIMER1( void )
{ // TIMER1 overflows at 65536 clocks
T1CON = 0; // stop TIMER1
TMR1IE = 0; // disable TIMER1 interrupt for now
T1GCON = 0; // disable TIMER1 gate input
TMR1H = 0; // set TIMER1 initial count value
TMR1L = 0;
//TICON settings TICKPS,TMR1CS don't work with 9.80
//TMR1ON =TICON<0>=0 TMR1ON=off
//T1RD16 =TICON<1>=0 do not set TMR1 H,L with one 16bit word
//T1SYNC =TICON<2>=1 do not sync external clock input with system clock(either way works the same for this pgm)
// T1SYNC=0 was setting in SETINT
//TISOSCEN=TICON<3>=0 Dedicated 2ndary Osc disabled
//TMR1CKPS=TICON<5:4>=0x10 1/4 prescaler of system clock
//TMR1CS =TICON<7:6>=0x01 TMR1 uses system clock,0x00=sysclk/4 instructClk
T1CON = 0b0110100;
TMR1IP = 1; // select high priority interrupt level //
TMR1IF = 0; // clear TIMER1 interrupt request //
TMR1IE = 1; // enable TIMER1 interrupt //
PEIE = 1; // enable peripheral interrupts //
TMR1ON = 1; // start TIMER1 //
}
void Init_PIC( void )
{
INTCON = 0; // disable interrupts //
INTCON2 = 0xF5;
INTCON3 = 0xC0;
PIE1 = 0;
PIE2 = 0;
PIE3 = 0;
OSCTUNE=0x1F; //Highest Internal OSC Freq OSCTUNE<0:5> set freq.
//OSCTUNE<5:0>set freq 011111 =16MHz PLLEN=bit6
// Turn on the PLL //
//OSCTUNEbits.PLLEN = 1; //also works 9.80
PLLEN=1; //if=1 Fosc=Fosc*4
//OSCCON =0x70; //Selects Int Osc Block 16Mhz internal clock source
//OSCCON bit7 IDLEN=0 Enter Sleep,=1 Enter Idle
//OSCCON<1:0> SCS..SCS=1x IntSysClk, 01=SecOsc SOSC,00=Clk set by CONFIG1
OSCCON = 0b01110000; // Enter Sleep mode for sleep, not IDLE
// set internal oscillator to 16MHz,
// use primary clock set by CONFIG1H<3:0>
// disable ADC inputs //
ANSELA = 0;
ANSELB = 0;
ANSELC = 0;
ANSELD = 0;
ANSELE = 0;
// disable Comparators //
CM1CON0 = 0;
CM2CON0 = 0;
// make all available GPIOs outputs and set low //
LATA = 0x00;
TRISA = 0x00; // originally RA6,RA7 as inputs //
LATB = 0x01;
TRISB = 0x01; //INT0 Sw on bbit0
LATC = 0x00;
TRISC = 0x00; // originally RC0,RC1 as inputs //
LATD = 0x00;
TRISD = 0x00;
LATE = 0x00;
TRISE = TRISE & ~0x07; // touch only PORTE direction bits //
}
//------------------------------main-----------------------------------------
void main(void)
{
Init_PIC();
Init_TIMER1();
IPEN = 1; //Priority interrupts enabled
//RCONbits.IPEN = 1; //also works 9.80
LATB =1;
TRISB =1; //INT0IE sw on bbit0
INT0IF=0;
INT0IE=1;
PEIE=1; //Needed for TMRx interrupts
//PIE1=1; //Not Needed for TMRx
// PIE2=1; //Not Needed for TMRx
GIEL=1;
GIEH=1; //Ok also to use INTCONbits.GIE = 1; to enable all interrupts
//===================================================================
Start:
for(ttemp=0;ttemp<60000;ttemp++)//a few secs, timed loop at 16MHz *4
{ INT0IE=1;
bbit1 =1; //Hey, why aren't you blinking!
//bbit2 blinks just fine, as it is supposed to
bbit5 =1;
bbit6 =1;
bbit7 =1;
TMR1IP=1;
}
for(ttemp=0;ttemp<60000;ttemp++) //a few seconds at 16MHz *4
{
INT0IE=0;
bbit1 =0;
bbit5 =0;
bbit6 =0;
bbit7 =0;
TMR1IP=0;
}
goto Start;
}