interapt u kodu ti ne radi, pretpostavljam nesto sa uart, vrlo lose ti je sto stavljas delay u interapt, a program se zaglavi u while u funkciji temperatura. Tako kaze mplabx simulator.
Code:
#include "htc.h"
//#include "delay.h"
#include "math.h"
unsigned int i;
unsigned int k;
unsigned char temp;
//unsigned int tabela[20]={277,332,391,452,512,574,631,683,730,772,810,843,870,893,913,930,945,957,967,975};
unsigned char j,razlika,vrednost,tabela;
void Init_RS232(unsigned int baud_rate,unsigned int oscilator_MHz)
{
unsigned int baud;
unsigned long vrednost_reg;
unsigned long fcy;
//podesavanje baudrate=====================
baud=baud_rate;
fcy=oscilator_MHz*1000000;
vrednost_reg=fcy/baud/64; //formula
vrednost_reg=vrednost_reg-1; //formula
SPBRG=vrednost_reg&0x00FF; //registri za baud
SPBRGH=(vrednost_reg&0xFF00)>>8;
BRG16=0; //8mo bitni baud generator
//BRGH_bit=1; //visok baud //ovo se ne sme staviti
//=========================================
//podesavanje rs232modula===================
GIE=1; //globalni interrupt
PEIE=1; //periferijski interrupt
SYNC=0; //asinhroni mod
SPEN=1; //omogucuje serijski modul;
//podesavanje transmitera==================
//TXIE_bit=1; //omogucen interrupt za TX
TXEN=0; //onemoguceno slanje
//======================================
//podesavanje prijemnika================
RCIE=1; //omogucen RX interrupt
CREN=1; //omogucuje recivera
//=======================================
RCIF=0; //================dodato====================
TXIE=1; //================dodato====================
}
unsigned char RS232_Read(void)
{
unsigned char read_buf;
read_buf=RCREG;
CREN=1;
return read_buf;
}
void RS232_Write(unsigned int podatak)
{
unsigned char prijem;
prijem=podatak;
TXREG=podatak;
// DelayUs(1000);
TXEN=1;
//DelayUs(1000);
TXEN=0;
TXREG=0;
}
void PWM1_Init(unsigned int PWM_frekvencija_Hz,unsigned char Frekvencija_oscilatora_MHz){
unsigned int fre;
unsigned char osc;
unsigned char perioda;
unsigned int obrada;
unsigned int ispuna;
osc=Frekvencija_oscilatora_MHz;
fre=PWM_frekvencija_Hz;
if(fre<620) fre=620;
if(fre>4800) fre=4800;
T2CON=0b00011010; //pres 16, pos 4
perioda=(osc*3906/(fre)); //perioda PWM signala
perioda=(perioda*4)-1;
PR2=perioda;
TMR2=0;
//GIE_bit=1;
//PEIE_bit=1;
//TMR2IE_bit=1;
TMR2IF=0;
CCP1CON=0x0C; //PWM mod, ne invertovan
ispuna=30; //inicijalno ugasen PWM
obrada=ispuna; //popunjavanje registara
ispuna=ispuna&0x03;
ispuna=ispuna<<4;
CCP1CON=0x0C|ispuna;
CCPR1L=obrada>>2;
TRISC2=0;
RC2=0;
TMR2ON=1;
}
void PWM1_Set_Parameters(unsigned int Frekvencija_Hz,unsigned char Procenat_ispune,unsigned char Frekvencija_oscilatora_MHz){
unsigned int fre;
unsigned char osc;
unsigned int perioda;
unsigned int rezolucija;
unsigned char osnova;
unsigned char dodavanje;
unsigned int ispuna_max;
unsigned int ispuna;
unsigned int max;
unsigned int obrada;
unsigned char procenat;
osc=Frekvencija_oscilatora_MHz;
fre=Frekvencija_Hz;
if(fre<620) fre=620;
if(fre>4800) fre=4800;
procenat=Procenat_ispune;
perioda=(osc*3906/(fre));
perioda=(perioda*4)-1;
PR2=perioda;
rezolucija=(log10(4*(perioda+1))/log10(2))*10;
osnova=rezolucija/10;
dodavanje=rezolucija%10;
switch (osnova){
case 6: ispuna_max=64; break;
case 7: ispuna_max=128; break;
case 8: ispuna_max=256; break;
case 9: ispuna_max=512; break;
}
switch (dodavanje){
case 1: max=ispuna_max*1.07177; break;
case 2: max=ispuna_max*1.14869; break;
case 3: max=ispuna_max*1.23144; break;
case 4: max=ispuna_max*1.31951; break;
case 5: max=ispuna_max*1.41421; break;
case 6: max=ispuna_max*1.51571; break;
case 7: max=ispuna_max*1.62450; break;
case 8: max=ispuna_max*1.74110; break;
case 9: max=ispuna_max*1.86607; break;
}
ispuna=procenat*max/100;
obrada=ispuna;
ispuna=ispuna&0x03;
ispuna=ispuna<<4;
CCP1CON=0x0C|ispuna;
CCPR1L=obrada>>2;
}
void Ispis_displej(unsigned char podatak){
unsigned char obrada,jedinica,desetica;
obrada=podatak;
jedinica=obrada%10;
desetica=obrada/10;
RD2=jedinica;
RD3=jedinica>>1;
RD4=jedinica>>2;
RD5=jedinica>>3;
RD1=1;
//DelayUs(1000);
RD1=0;
RD2=desetica;
RD3=desetica>>1;
RD4=desetica>>2;
RD5=desetica>>3;
RD0=1;
//DelayUs(1000);
RD0=0;
}
void Slanje(void)
{
TXEN=1;
}
static void interrupt
zile(void)
{
if(TXIF)
{
// DelayUs(255);
RS232_Write(5|0x30);
//DelayUs(255);
}
}
//dodato===================================
char temperatura(int temp)
{
i=0;
while(temp>=tabela) i++;
razlika=tabela;
i--;
razlika=razlika-tabela;
razlika=razlika/5;
j=0;
while(temp>=(tabela+razlika*j)) j++;
j--;
vrednost=5+i*5+j;
if(temp>tabela) vrednost=0;
if(temp<tabela) vrednost=0;
return vrednost;
}
//=========================================
void main(){
OPTION_REG=0;
INTCON=0;
ANSEL=0;
ANSELH=0;
TRISA=0;
TRISB=0;
PORTA=0;
PORTB=0;
Init_RS232(9600,10);
PWM1_Init(2000,10);
PWM1_Set_Parameters(2000,10,10);
Ispis_displej(0);
i=0;
//DelayUs(255);
while(1){
//DelayUs(255);
PORTB=~PORTB;
i++;
temp=temperatura(760);
if(i==4000){
Slanje();
i=0;
k++;
Ispis_displej(temp);
}
}
}
Ovo ti je setovanje za 16f887
Code:
16F887 Support Information
#pragma config Usage
#pragma config <setting>=<named value>
For example:
// Data Code Protection bit: Data memory code protection is disabled
// Brown Out Reset Selection bits: BOR disabled
// Internal External Switchover bit: Internal/External Switchover mode is disabled
// In-Circuit Debugger Mode bit: In-Circuit Debugger disabled, RB6/ICSPCLK and RB7/ICSPDAT are general purpose I/O pins
// Oscillator Selection bits: INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
// Fail-Safe Clock Monitor Enabled bit: Fail-Safe Clock Monitor is disabled
// RE3/MCLR pin function select bit: RE3/MCLR pin function is digital input, MCLR internally tied to VDD
// Watchdog Timer Enable bit: WDT disabled and can be enabled by SWDTEN bit of the WDTCON register
// Code Protection bit: Program memory code protection is disabled
// Low Voltage Programming Enable bit: RB3 pin has digital I/O, HV on MCLR must be used for programming
// Power-up Timer Enable bit: PWRT disabled
#pragma config CPD = OFF, BOREN = OFF, IESO = OFF, DEBUG = OFF, FOSC = INTRC_NOCLKOUT, FCMEN = OFF, MCLRE = OFF, WDTE = OFF, CP = OFF, LVP = OFF, PWRTE = OFF
#pragma config <setting>=<literal constant>
For example:
// Data Code Protection bit: Data memory code protection is disabled
// Brown Out Reset Selection bits: BOR disabled
// Internal External Switchover bit: Internal/External Switchover mode is disabled
// In-Circuit Debugger Mode bit: In-Circuit Debugger disabled, RB6/ICSPCLK and RB7/ICSPDAT are general purpose I/O pins
// Oscillator Selection bits: INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
// Fail-Safe Clock Monitor Enabled bit: Fail-Safe Clock Monitor is disabled
// RE3/MCLR pin function select bit: RE3/MCLR pin function is digital input, MCLR internally tied to VDD
// Watchdog Timer Enable bit: WDT disabled and can be enabled by SWDTEN bit of the WDTCON register
// Code Protection bit: Program memory code protection is disabled
// Low Voltage Programming Enable bit: RB3 pin has digital I/O, HV on MCLR must be used for programming
// Power-up Timer Enable bit: PWRT disabled
#pragma config CPD = 0x1, BOREN = 0x0, IESO = 0x0, DEBUG = 0x1, FOSC = 0x4, FCMEN = 0x0, MCLRE = 0x0, WDTE = 0x0, CP = 0x1, LVP = 0x0, PWRTE = 0x1
#pragma config <register>=<literal constant>
For example:
// Data Code Protection bit: Data memory code protection is disabled
// Brown Out Reset Selection bits: BOR disabled
// Internal External Switchover bit: Internal/External Switchover mode is disabled
// In-Circuit Debugger Mode bit: In-Circuit Debugger disabled, RB6/ICSPCLK and RB7/ICSPDAT are general purpose I/O pins
// Oscillator Selection bits: INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
// Fail-Safe Clock Monitor Enabled bit: Fail-Safe Clock Monitor is disabled
// RE3/MCLR pin function select bit: RE3/MCLR pin function is digital input, MCLR internally tied to VDD
// Watchdog Timer Enable bit: WDT disabled and can be enabled by SWDTEN bit of the WDTCON register
// Code Protection bit: Program memory code protection is disabled
// Low Voltage Programming Enable bit: RB3 pin has digital I/O, HV on MCLR must be used for programming
// Power-up Timer Enable bit: PWRT disabled
#pragma config CONFIG1 = 0xE0D4
For example:
// IDLOC @ 0x2000
#pragma config IDLOC0 = 0x3FFF
#pragma config Settings
Register: CONFIG1 @ 0x2007
CPD = Data Code Protection bit
OFF Data memory code protection is disabled
ON Data memory code protection is enabled
BOREN = Brown Out Reset Selection bits
OFF BOR disabled
ON BOR enabled
NSLEEP BOR enabled during operation and disabled in Sleep
SBODEN BOR controlled by SBOREN bit of the PCON register
IESO = Internal External Switchover bit
OFF Internal/External Switchover mode is disabled
ON Internal/External Switchover mode is enabled
DEBUG = In-Circuit Debugger Mode bit
OFF In-Circuit Debugger disabled, RB6/ICSPCLK and RB7/ICSPDAT are general purpose I/O pins
ON In_Circuit Debugger enabled, RB6/ICSPCLK and RB7/ICSPDAT are dedicated to the debugger
FOSC = Oscillator Selection bits
INTRC_NOCLKOUT INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
XT XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN
LP LP oscillator: Low-power crystal on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN
INTRC_CLKOUT INTOSC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
EXTRC_CLKOUT RC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, RC on RA7/OSC1/CLKIN
EXTRC_NOCLKOUT RCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, RC on RA7/OSC1/CLKIN
EC EC: I/O function on RA6/OSC2/CLKOUT pin, CLKIN on RA7/OSC1/CLKIN
HS HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN
FCMEN = Fail-Safe Clock Monitor Enabled bit
OFF Fail-Safe Clock Monitor is disabled
ON Fail-Safe Clock Monitor is enabled
MCLRE = RE3/MCLR pin function select bit
OFF RE3/MCLR pin function is digital input, MCLR internally tied to VDD
ON RE3/MCLR pin function is MCLR
WDTE = Watchdog Timer Enable bit
OFF WDT disabled and can be enabled by SWDTEN bit of the WDTCON register
ON WDT enabled
CP = Code Protection bit
OFF Program memory code protection is disabled
ON Program memory code protection is enabled
LVP = Low Voltage Programming Enable bit
OFF RB3 pin has digital I/O, HV on MCLR must be used for programming
ON RB3/PGM pin has PGM function, low voltage programming enabled
PWRTE = Power-up Timer Enable bit
OFF PWRT disabled
ON PWRT enabled
Register: CONFIG2 @ 0x2008
BOR4V = Brown-out Reset Selection bit
BOR40V Brown-out Reset set to 4.0V
BOR21V Brown-out Reset set to 2.1V
WRT = Flash Program Memory Self Write Enable bits
HALF 0000h to 0FFFh write protected, 1000h to 1FFFh may be modified by EECON control
OFF Write protection off
1FOURTH 0000h to 07FFh write protected, 0800h to 1FFFh may be modified by EECON control
256 0000h to 00FFh write protected, 0100h to 1FFFh may be modified by EECON control
Register: IDLOC0 @ 0x2000
Register: IDLOC1 @ 0x2001
Register: IDLOC2 @ 0x2002
Register: IDLOC3 @ 0x2003