Hi,
Know we have been here before, and i have figured it out somehow, but now it's struggeling to much,
Have this small setup.
The name on each port is not correct, but portnumber is, Due to missing footprint.
I am working on to let i run precise 1 sec on each roolover, by using the 32.768 x-tal.
Did have a logicanalyser from PICKIT2 to see that signal is comming from x-tal.
But i know it's some setup that is killing me,
Have tried for days, a studing datasheet many times,
Here is my code.
it's killing me....
Know we have been here before, and i have figured it out somehow, but now it's struggeling to much,
Have this small setup.
The name on each port is not correct, but portnumber is, Due to missing footprint.
I am working on to let i run precise 1 sec on each roolover, by using the 32.768 x-tal.
Did have a logicanalyser from PICKIT2 to see that signal is comming from x-tal.
But i know it's some setup that is killing me,
Have tried for days, a studing datasheet many times,
Here is my code.
Code:
/*
* File: newmain1.c
* Author: Me self
* Created on 17. maj 2020, 13:26
*/
//#include <stdlib.h>
#include <xc.h>
#include <stdbool.h>
#include <stdint.h>
// CONFIG1
#pragma config FEXTOSC = OFF // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits (HFINTOSC with OSCFRQ= 32 MHz and CDIV = 1:1)
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)
// CONFIG2
#pragma config MCLRE = ON // Master Clear Enable bit->MCLR pin is Master Clear function
#pragma config PWRTE = OFF // Power-up Timer Enable bit->PWRT disabled
#pragma config LPBOREN = OFF // Low-Power BOR enable bit->ULPBOR disabled
#pragma config BOREN = ON // Brown-out reset enable bits->Brown-out Reset Enabled, SBOREN bit is ignored
#pragma config BORV = LO // Brown-out Reset Voltage Selection->Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices
#pragma config ZCD = OFF // Zero-cross detect disable->Zero-cross detect circuit is disabled at POR.
#pragma config PPS1WAY = ON // Peripheral Pin Select one-way control->The PPSLOCK bit can be cleared and set only once in software
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit->Stack Overflow or Underflow will cause a reset
// CONFIG3
#pragma config WDTCPS = WDTCPS_31 // WDT Period Select bits->Divider ratio 1:65536; software control of WDTPS
#pragma config WDTE = OFF // WDT operating mode->WDT Disabled, SWDTEN is ignored
#pragma config WDTCWS = WDTCWS_7 // WDT Window Select bits->window always open (100%); software control; keyed access not required
#pragma config WDTCCS = SC // WDT input clock selector->Software Control
// CONFIG4
#pragma config BBSIZE = BB512 // ->512 words boot block size
#pragma config BBEN = OFF // ->Boot Block disabled
#pragma config SAFEN = OFF // ->SAF disabled
#pragma config WRTAPP = OFF // ->Application Block not write protected
#pragma config WRTB = OFF // ->Boot Block not write protected
#pragma config WRTC = OFF // ->Configuration Register not write protected
#pragma config WRTSAF = OFF // ->SAF not write protected
#pragma config LVP = ON // Low Voltage Programming Enable bit->Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.
// CONFIG5
#pragma config CP = OFF // UserNVM Program memory code protection bit->UserNVM code protection disabled
#define _XTAL_FREQ 32000000
volatile uint16_t timer1ReloadVal;
unsigned int sekund,minut,time,Hour;
char BT_Modtag,Newsecond;
uint8_t nytsekund,no70_opens,no80_opens,runs_code70,ReadyToCode70,runs_code80,ReadyToCode80,no90_opens,runs_code90,ReadyToCode90;
uint8_t ContrastValue,trash,NightContrastTime,DayContrastTime;
uint8_t BT_Data = 0;
void setup_system (void)
{
PMD0 = 0x45;
PMD1 = 0x05;
PMD2 = 0x67;
PMD3 = 0x3F;
PMD4 = 0x00;
PMD5 = 0x1E;
LATA = 0x00;
LATB = 0x00;
LATC = 0x00;
TRISA = 0xFF; // all input
TRISB = 0xFB; // RB2=out to BT all other IN
TRISC = 0xAE;//10101110
ANSELC = 0x03;
ANSELB = 0x00;
ANSELA = 0x00;
WPUB = 0x00;
WPUA = 0x00;
WPUC = 0x00;
ODCONA = 0x00;
ODCONB = 0x00;
ODCONC = 0x00;
SLRCONA = 0xFF;
SLRCONB = 0xFF;
SLRCONC = 0xFF;
INLVLA = 0xFF;
INLVLB = 0xFF;
INLVLC = 0xFF;
RB2PPS = 0x11; //RB2->EUSART2:TX2;
RC6PPS = 0x0F; //RC6->EUSART1:TX1;
RX2DTPPS = 0x09; //RB1->EUSART2:RX2;
RX1DTPPS = 0x17; //RC7->EUSART1:RX1;
OSCCON1 = 0x60;// NOSC HFINTOSC; NDIV 4;
OSCCON3 = 0x00; // CSWHOLD may proceed; SOSCPWR Low power;
OSCEN = 0x08; // MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
OSCFRQ = 0x06; // HFFRQ 32_MHz;
OSCSTAT = 0x00;// MFOR not ready;
OSCTUNE = 0x00; // HFTUN 0;
SOSCEN=1; // set secondary osc allways enable
T1GCON = 0x00; //T1GE disabled; T1GTM disabled; T1GPOL low; T1GGO done; T1GSPM disabled;
T1GATE = 0x00;//GSS T1G_pin;
T1CLK = 0x07; //CS SOSC;
TMR1H = 0x80; //TMR1H 128;
TMR1L = 0x00; //TMR1L 0;
// Clearing IF flag before enabling the interrupt.
TMR1IF = 0;
// Load the TMR value to reload variable
timer1ReloadVal=(uint16_t)((TMR1H << 8) | TMR1L);
// Enabling TMR1 interrupt.
TMR1IE = 1;
T1CON = 0x05;// CKPS 1:1; nT1SYNC synchronize; TMR1ON enabled; T1RD16 disabled;
//SETUP EUSART1
PIE3bits.TX1IE = 0;
// Set the EUSART1 module to the options selected in the user interface.
BAUD1CON = 0x00; // ABDOVF no_overflow; SCKP Non-Inverted; BRG16 8bit_generator; WUE disabled; ABDEN disabled;
RC1STA = 0x90;// SPEN enabled; RX9 8-bit; CREN enabled; ADDEN disabled; SREN disabled;
TX1STA = 0x24; // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave;
SP1BRGL = 0xBF; // SP1BRGL 191; BAUD 10417
SP1BRGH = 0x00;// SP1BRGH 0;
// SETUP EUSART2 BT
RC2IE = 0;
TX2IE = 0;
BAUD2CON = 0x00;// ABDOVF no_overflow; SCKP Non-Inverted; BRG16 8bit_generator; WUE disabled; ABDEN disabled;
RC2STA = 0x90;// SPEN enabled; RX9 8-bit; CREN enabled; ADDEN disabled; SREN disabled;
TX2STA = 0x24; // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave;
SP2BRGL = 0xCF; // SP2BRGL 207; // BAUD 9600 standard
SP2BRGH = 0x00; // SP2BRGH 0;
GIE=1; // global interrupt
PEIE=1;
TMR1IE=1;
}
void __interrupt()ISR (void)
{
if ((TMR1IE == 1) && (TMR1IF == 1))
{
TMR1IF = 0;
TMR1H = (timer1ReloadVal >> 8);
TMR1L = timer1ReloadVal;
nytsekund=1; // tell main loop that new second is gone.
}
}
void EUSART1_Write(uint8_t txData)
{
TX1REG = txData;
}
uint8_t EUSART2_Read(void)
{
//uint8_t readValue = 0;
return RC2REG;
}
void SendTimeToSlave (void)
{ // do something later.
}
void main(void)
{
setup_system();
time=0;
sekund=0;
minut=0;
RC4=!RC4; // test only....
while(1)
{
// if new second
if (nytsekund==1)
{
RC4=!RC4; // test only
sekund++; // add one
nytsekund=0; // clears the bit
if (sekund==60) { // if one minut gone
sekund=0; // sets second to zero
minut++; // add 1 minute
if (minut==60) // if minut is 60,
{
minut=0; // minut is zero
time++; // add to hour
if(time==24)time=0; // if midnight hours is zero
}//end minut
} //end sekund
//send to all other.
SendTimeToSlave(); // empty yet
}//end nyt sekund
}// end while
}