刚把程序给调过了,设置也改成如下图所示:
在release模式下的仿真,仿真时也能进入仿真界面,但是源程序窗口不显示程序执行到哪儿了(小箭头),只在dissembly里面执行完main的前几句就卡在spi_send_byte了,求高手指点!
这是刚开始时的
这是卡住时的
这是一个电力线载波的程序,连接为:
电力线<——>电力线载波调制解调芯片<——>SPI通信<——>mega16<——>UART通信<——>PC
如果是程序有错,也希望能不吝赐教!
源程序如下:
/*******************************************************************************
* Filename: main.c
* Description: This program acts as a bridge from SPI to PC and PC
* to SPI.
* Note:
*******************************************************************************/
#include
#include
#include
#include "UART.h"
#include "SPI.h"
/*------------------------------------------------------------------------------
Global variables declaration
------------------------------------------------------------------------------*/
extern unsigned char UART_tx_buffer[UART_TX_BUFFER_SIZE];
extern unsigned char UART_tx_wr_index,UART_tx_rd_index,UART_tx_counter;
extern unsigned char UART_rx_buffer[UART_RX_BUFFER_SIZE];
extern unsigned char UART_rx_wr_index,UART_rx_rd_index,UART_rx_counter;
extern unsigned char Uart_RecvFlag,UART_rx_counter_temp;
extern unsigned char SPI_rx_buffer[SIZE];
extern unsigned char SPI_tx_buffer[SIZE];
extern unsigned char SPI_rx_wr_index,SPI_rx_rd_index,SPI_rx_counter,SPI_rx_buffer_overflow;
extern unsigned char SPI_tx_wr_index,SPI_tx_rd_index,SPI_tx_counter;
extern unsigned char SPI_free,SPI_RecvFlag;
extern void spi_tx_packet(void);
unsigned char mode_state;
/*******************************************************************************
* Name: delay_ms(unsigned int n)
* Description: delay ms.
* Parameters: n.
*******************************************************************************/
void delay_ms(unsigned int n) //---->ms延时函数
{
unsigned int i;
while(n--)
{
for(i=1333;i>0;i--) asm("nop"); //1时钟周期
}
}
//时钟16MHz 周期62.5ns
/*******************************************************************************
* Name: uart_to_spi(void)
* Description: Transmits the bytes from uart buffer to spi buffer.
*******************************************************************************/
void uart_to_spi(void)
{
while(SPI_tx_counter >= SIZE); //SPI发送缓冲区满,等待
__disable_interrupt();
if(Uart_RecvFlag)
{ //数据包接收完成
while(UART_rx_counter && (SPI_tx_counter <= UART_RX_BUFFER_SIZE))
{
SPI_tx_buffer[SPI_tx_wr_index] = UART_rx_buffer[UART_rx_rd_index];
UART_rx_counter--;
SPI_tx_counter++;
INCR(SPI_tx_wr_index,SIZE);
INCR(UART_rx_rd_index,UART_RX_BUFFER_SIZE);
}
}
__enable_interrupt();
}
/*******************************************************************************
* Name: spi_to_uart(void)
* Description: Transmits the bytes from spi buffer to uart buffer.
*******************************************************************************/
void spi_to_uart(void)
{
while(UART_tx_counter >= UART_TX_BUFFER_SIZE); //UART发送缓冲区满,等待
if(SPSR & (1 << SPIF))
{
while(SPI_rx_counter && (UART_tx_counter < UART_TX_BUFFER_SIZE)) //SPI接收缓冲区有数且UART发送缓冲区未溢出
{
UART_tx_buffer[UART_tx_wr_index] = SPI_rx_buffer[SPI_rx_rd_index]; //将SPI接收到的byte移至UART发送缓冲区
__disable_interrupt();
SPI_rx_counter--;
UART_tx_counter++;
INCR(SPI_rx_rd_index,SIZE);
INCR(UART_tx_wr_index,UART_TX_BUFFER_SIZE);
__enable_interrupt();
}
}
}
/*******************************************************************************
* Name: main()
* Description: Main function.
* Parameters: None.
* Return: Exit code (should never return).
* Note:
*******************************************************************************/
void main(void)
{
delay_ms(50);
mode_state = 0x00;
static unsigned char configuration[3] = {0xA3,0x11,0xBA};
__disable_interrupt();
// Initialize SPI port.
spi_init();
// Initialization of Serial port.
uart_init();
// Initialize interrupts (SPI interrupt is mandatory for PLM-1 configuration).
//int_init();
// Initialization of ST7538 with configuration Bytes.
unsigned char i;
mode_CR_wr();
for(i=3;i>0;i--) spi_send_byte(configuration );
__enable_interrupt();
while(1)
{
/* Powerline -> RS232 */
//从电力线接收数据
mode_data_rx(); //进入数据接收模式
if(carrier_appear)
{ //载波出现
PORTB |= 0x08; //则CD/PD被置0,进而将nSS拉低
SPDR = to_read_byte;
if(SPI_RecvFlag) // 传输完成则进入中断,直到一个数据包接收完成
spi_to_uart();
if(UART_tx_counter)
uart_tx_packet();
}
else
PORTB &= 0xF7; //载波消失,nSS置位
/* RS232 -> Powerline */
if(Uart_RecvFlag) //UART接收完成
{
/* Get received packet from PLM-1 library. */
if(UART_rx_counter) uart_to_spi(); //发送uart接收到的数据到spi_tx_buffer
mode_data_tx(); //数据发送
if(SPI_tx_counter)
{
spi_tx_packet();
}
}
}
}
暂无评论,赶紧抢沙发吧