• slider image 514
  • slider image 516
  • slider image 517
  • slider image 518
  • slider image 519
:::


Browsing this Thread:   1 Anonymous Users






當Timer0和Rx寫在一起卻不動作(思想陷在死迴圈)
#1
初級會員
初級會員


查看用戶資訊
我已經陷入不知道該怎麼改的地步了,嘗試過把高低中斷互換也沒用><~ 懇請高手幫忙協助,感激萬分。

程式主要的問題在於,當有資料經由RX送進來時,但RX的中斷卻無發生。不要說顯示第2Byte的值,我光連0xAA都顯示不出。(應該是RX中斷都沒發生)

程式構想如下:
程式會判別ADC值,一開始
1.當ADC>0x33,PORTCbits.RC2燈亮、PORTCbits.RC1燈滅(並送出USART:0x31)。
2.當持續5秒後,ADC>0x33。PORTCbits.RC2燈滅、PORTCbits.RC1燈亮(並送出USART:0x32)。
3.當ADC<0x19,PORTCbits.RC2燈亮、PORTCbits.RC1燈亮(並送出USART:0x33)。
4.當持續10秒後,ADC<0x19。PORTCbits.RC2燈滅、PORTCbits.RC1燈滅(並送出USART:0x34)。

但只要接受到RX中斷,PORTB的燈就會按照接收的第二BYTE顯示。
----------------------------------------------

#include <p18f2321.h>
#include <delays.h>
#include <usart.h>
#include <adc.h>
#include <timers.h>


#pragma config OSC=HS, BOR=OFF, BORV=2, PWRT=ON, WDT=OFF, LVP=OFF

#define TMR0_VAL 65536-9840


struct Flag
{
unsigned START:1;
}FLAGbits;

unsigned char rxbuf[1];
unsigned char rxbuf_index;
unsigned char ADC_TEMP;
unsigned int time_cnt;

void Init_TMR0(void);
void timer0_isr (void);
void Rx_isr(void);
void Init_USART(void);
void Init_MCU(void);
void ADC_CKLEVEL(void);
void shutter_dip(void);


#pragma code low_vector=0x18
void low_interrupt(void)
{
_asm GOTO Rx_isr _endasm
}
#pragma code

#pragma interrupt Rx_isr

void Rx_isr (void)
{
unsigned char RX_Temp;
PIR1bits.RCIF = 0;
PORTB=0xAA;
rxbuf[rxbuf_index] = ReadUSART();
rxbuf_index++;

if(!(rxbuf_index<2))
{
PORTB = rxbuf[1];
rxbuf_index=0;
}
}

#pragma code high_vector=0x08
void high_interrupt (void)
{
_asm GOTO timer0_isr _endasm
}
#pragma code

#pragma interrupt timer0_isr

void timer0_isr (void)
{
INTCONbits.TMR0IF=0;
WriteTimer0(TMR0_VAL);

if(time_cnt>0)
time_cnt--;
}


void main (void)
{
Init_MCU();
Init_USART();
Init_TMR0();
TRISC = 0x00;
//PORTC=0xFF;

INTCONbits.TMR0IF=0;
INTCON2bits.TMR0IP=1;

RCONbits.IPEN=1;
INTCONbits.GIEL=1;
INTCONbits.GIEH=1;

WriteUSART(0x00);
while(BusyUSART());

TRISB=0x00;
PORTB=0x00;
rxbuf_index=0;
ADC_TEMP=1;

while (1)
{
switch(ADC_TEMP)
{
case 1:
{
Delay10TCYx(10);
ConvertADC() ;
while(BusyADC());

if(ADRESH>=0x33)
{
time_cnt=500;
ADC_TEMP=2;
PORTCbits.RC2=0;
PORTCbits.RC1=1;
WriteUSART(0x31);
while(BusyUSART());
}
}
break;

case 2:
{


Delay10TCYx(10);
ConvertADC() ;
while(BusyADC());
if(ADRESH>=0x33)
{
if(time_cnt==0)
{
ADC_TEMP=3;
PORTCbits.RC2=1;
PORTCbits.RC1=0;
WriteUSART(0x32);
while(BusyUSART());
}
}
else
ADC_TEMP=1;
}
break;

case 3:
{

Delay10TCYx(10);
ConvertADC() ;
while(BusyADC());

if(ADRESH<=0x19)
{
time_cnt=1000;
ADC_TEMP=4;
PORTCbits.RC2=0;
PORTCbits.RC1=0;
WriteUSART(0x33);
while(BusyUSART());

}
}
break;

case 4:
{
Delay10TCYx(10);
ConvertADC() ;
while(BusyADC());
if(ADRESH<=0x19)
{
if(time_cnt==0)
{
ADC_TEMP=1;
PORTCbits.RC2=1;
PORTCbits.RC1=1;
WriteUSART(0x34);
while(BusyUSART());
}
}
else
ADC_TEMP=3;
}
break;
}//SWITCH ADC
}//while
}

void Init_MCU(void)
{
ADCON0=0x05; //CH1:05 CH0:01
ADCON1=0x0D; //A:AN0、AN1
ADCON2=0x3A;

TRISA=0xFF;
TRISB=0xFF;
TRISC=0XB9;
//TRISCbits.TRISC6=0;
//TRISCbits.TRISC7=1;

PORTA=0xFF;
PORTB=0xFF;
PORTC=0xF9;

FLAGbits.START=0;
}

void Init_USART(void)
{
OpenUSART( USART_TX_INT_OFF &
USART_RX_INT_ON &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH,
12 );
PIR1bits.RCIF=0;
IPR1bits.RCIP=0;
}

void Init_TMR0 (void)
{
OpenTimer0( TIMER_INT_ON &
T0_16BIT &
T0_SOURCE_INT &
T0_EDGE_RISE &
T0_PS_1_1 );

WriteTimer0(TMR0_VAL);
INTCONbits.TMR0IF=0;
INTCON2bits.TMR0IP=0;

INTCONbits.PEIE = 1;
INTCONbits.GIE = 1;
}

發表於: 2008/10/18 16:15
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部







You can view topic.
不可以 發起新主題
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.

[進階搜尋]


:::

Microchip連結

https://www.facebook.com/microchiptechnologytaiwan/
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=13
https://mu.microchip.com/page/tmu
http://elearning.microchip.com.tw/modules/tad_link/index.php?cate_sn=1
https://page.microchip.com/APAC-PrefCenters-TW.html
http://www.microchip.com/
http://www.microchip.com/treelink
http://www.microchipdirect.com/
http://www.microchip.com.cn/newcommunity/index.php?m=Video&a=index&id=103
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=2
http://www.microchip.com.tw/Data_CD/eLearning/index.html
http://www.microchip.com.tw/RTC/RTC_DVD/
https://www.microchip.com/development-tools/
https://www.youtube.com/user/MicrochipTechnology
[ more... ]

教育訓練中心

!開發工具購買
辦法說明 [業界客戶] [教育單位]
----------------------------------
!校園樣品申請
辦法說明 [教師資格] [學生資格]
----------------------------------