⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cyan_base_driver.c.bak

📁 使用CYAN单片机的ECOG1开发板连接SIM300C的GSM模块开发的短信收发程序.供UART和AT指令编程者参考
💻 BAK
字号:
/******************************************************************************
MODE:		AML--#GPS+accelerator+compass
MCU:		Cyan eCOG1k
COMPILER:	CyanIDE v1.41
FILE NAME:	cyan_base_driver C source code files
******************************************************************************/
#define cyan_base_driver_code

/******************************************************************************
include all head files
******************************************************************************/
#include "cyan_base_driver.h"

/******************************************************************************
Declaration of static functions.
******************************************************************************/
void delay_2us(unsigned int us_reg);
void delay_ms(unsigned int ms_reg);
unsigned int cyan_spi_transfer_byte(unsigned int mosi);
void cyan_spi_assert_CS(spi_CS_type _CS_);
void cyan_spi_deassert_CS(spi_CS_type _CS_);
int putchar(int c);
void uart_initialization(void);
void timer_initialization(void);
extern void __irq_code GPS_data_transmit(void);
extern void __irq_code xy_data_transmit(void);
char *_fcvt_(char *str_save,double float_data);

extern void ACC_COM_insert_reading(void);
/******************************************************************************
FUNCTIONS
    dilay functions for cyan .

RETURNS
   The MISO signal that was received.
******************************************************************************/

//-----------------------------------------------------------------------------
//delay us @5MHz
//-----------------------------------------------------------------------------
void delay_2us(unsigned int us_reg)
{
	unsigned int uuu;			//delay time = 1*2us ~ 65535*2us.
	while(us_reg-- > 0)
	{
		uuu= 5;					//delay time = 2us.
		while(uuu-- >0){}
	}
}
//-----------------------------------------------------------------------------
//delay ms @5MHz
//-----------------------------------------------------------------------------
void delay_ms(unsigned int ms_reg)
{
	unsigned int mmm;			//delay time = 1ms ~ 65535ms.
	while(ms_reg-- > 0)
	{
		mmm = 2500;				//delay time = 1ms.
		while(mmm-- >0){}
	}
}

/******************************************************************************
FUNCTIONS
    Simultaneously transmits and receives one byte on the SPI.

RETURNS
   The MISO signal that was received.
******************************************************************************/

unsigned int cyan_spi_transfer_byte(unsigned int mosi)
{
    static unsigned int miso = 0;
    
    // Should not write/read to the _last registers in the tx/rx registers
        
    // Transmit the byte
    rg.dusart.a0_tx8 = mosi;
    
	while (fd.dusart.a_int_sts.tx0_rdy != 1)
		;
	while (fd.dusart.a_int_sts.rx0_1b_rdy != 1)
		;
    
    // Read the received data byte
    miso = rg.dusart.a0_rx8;
    
    return (miso);

}
//-----------------------------------------------------------------------------
// Asserts the SPI chip select signal CS0 for internal use
// and GPIOx for the external device chip select.
//-----------------------------------------------------------------------------
void cyan_spi_assert_CS(spi_CS_type _CS_)
{
    /* In this function, we use GPIO16 as the actual chip select.
     * The hardware does not allow the CS signal to stay asserted
     * for more than one single SPI transfer, so this is done manually.
     * Chip select 0 must be asserted internally to enable data to be received.
     * There are two problems in the hardware:
     *   1. CS0 must be asserted to enable data reception.
     *   2. CS does not stay asserted across byte boundaries.
     */
	
 	// Enable transmit and receive
    rg.dusart.spi_ctrl = DUSART_SPI_CTRL_RX_EN_MASK | DUSART_SPI_CTRL_TX_EN_MASK;
    
    switch(_CS_)
    {
    	case	accelerator_CS	:
    		accelerator_CS_Clr = 1;
    		break;
    		
    	case	compass_CS		:
    		break;
    	
    	default : break;
    }
    
    fd.dusart.spi_frame_ctrl.tx_slave_sel = 1;	// Assert CS0 internally
	
}
//-----------------------------------------------------------------------------
// Accelerator deasserts all chip selects.
//-----------------------------------------------------------------------------
void cyan_spi_deassert_CS(spi_CS_type _CS_)
{
    // See comments for spi_assert_cs()
    switch(_CS_)
    {
    	case	accelerator_CS	:
    		accelerator_CS_Set = 1;
    		break;
    		
    	case	compass_CS		:
    		break;
    	
    	default : break;
    }
    
    fd.dusart.spi_frame_ctrl.tx_slave_sel = 0;
	// Disable transmit and receive
    rg.dusart.spi_ctrl = DUSART_SPI_CTRL_RX_DIS_MASK | DUSART_SPI_CTRL_TX_DIS_MASK;   
}
/******************************************************************************
FUNCTION
   Implements the ANSI standard character output function. This version
   outputs a character through the DUART channel B and the LCD.

RETURNS
   The character.
******************************************************************************/
void __irq_entry usrb_tx_handler(void)
{
	while (0 == fd.duart.b_sts.tx_rdy)
		;
	xy_data_transmit();
	//GPS_data_transmit();
	fd.duart.b_int_dis.tx_rdy = 1;
	//rg.io.gp0_3_out = IO_GP0_3_OUT_CLR1_MASK | IO_GP0_3_OUT_SET1_MASK;
}

void __irq_entry usra_tx_handler(void)
{
	//GPS_data_transmit();
	//fd.duart.a_int_dis.tx_rdy = 1;
}

int putchar(int c)
{
	// Wait for the UART to be ready to transmit
    while (0 == fd.duart.b_sts.tx_rdy)
       ;

    rg.duart.b_tx8 = c;
    return (c);
}
/******************************************************************************
NAME
    uart_initialization

SYNOPSIS
    void uart_initialization(void)

FUNCTION
    initialization uarta
    
NOTE
    Duart clk ---> HIGH_PLL by8
    
	Baud 	Duart.a_baud	Reality baud	Tolerance
	1200		650			1200.1			0.01%
	2400		324			2403.8			0.2%
	3600		216			3600.2			0.01%
	4800		162			4792.9			0.2%
	7200		107			7233			0.5%
	9600		80			9645.1			0.5%
	19200		40			19054.9			0.8%
	38400		19			39062.5			1.7%
	57600		13			55803.6			3.1%
	115200		6			111607.1		3.1%
    
RETURNS
    Nothing
******************************************************************************/
void uart_initialization(void)
{
	fd.duart.ctrl.a_rx_en = 1;				//enable UARTA receiver.
	//fd.duart.ctrl.a_tx_en = 1;
	//fd.duart.ctrl.b_rx_en = 1;
	fd.duart.ctrl.b_tx_en = 1;
	
	fd.duart.a_int_en.rx_1b_rdy = 1;		// Enable duart A receive interrupt
	//fd.duart.b_int_dis.rx_1b_rdy = 1;		// Disable duart B receive interrupt
	fd.duart.b_int_en.tx_rdy = 1;		// Enable duart B tx interrupt
	
	fd.duart.b_tmr_cfg.guard = 63;			// Tx interval timer = 63 clock cycle
	
	rg.duart.a_baud = 162;					//baud = 4800;
	rg.duart.b_baud = 162;					//baud = 4800;
	
}
/******************************************************************************
** init Timer2
******************************************************************************/
void timer_initialization(void)
{
	//------------------------------------------------------------------------
	//timer 1
	//------------------------------------------------------------------------
	fd.tim.ctrl_en.cnt1_cnt = 1;//TIM_CTRL_EN_CNT1_CNT_MASK;
    fd.tim.int_en1.cnt1_exp = 1;//TIM_INT_EN1_CNT1_EXP_MASK;
	
	
	//------------------------------------------------------------------------
	//timer 2
	//------------------------------------------------------------------------
	fd.tim.ctrl_en.cnt2_cnt = 1;
	//fd.ssm.rst_clr.cnt2 = 1;			//clr timer2 reset.
	//fd.ssm.div_sel.cnt2 = 0;			//reference(high freq) clock,not PLL.
	
	//fd.ssm.clk_dis.cnt2 = 1;			//disable timer2 clock.
	//fd.ssm.tap_sel2.cnt2 = 0;			//timer2 Clock is divided by 4.
	//fd.ssm.clk_en.cnt2 = 1;				//enable timer2 clock.
	
	//fd.tim.int_en1.cnt2_exp = 1;		//enable timer2 interrupt.
	fd.tim.int_dis1.cnt2_exp = 1;		//disable timer2 interrupt.
	
	//fd.tim.ctrl_dis.cnt2_cnt = 1;		//disable Timer2.
	//fd.tim.ctrl_dis.cnt2_auto_re_ld = 1;//disable timer2 auto reload.
	//rg.tim.cnt2_ld = T2_LOAD_VALUE;		//timer2 load value.
	//fd.tim.cmd.cnt2_ld = 1;				//loading value to timer2.
	//fd.tim.int_clr1.cnt2_exp = 1;		// Clear Timer2 interrupt flag	
}
/******************************************************************************
float point change to string.
******************************************************************************/
char *_fcvt_(char *str_save,double float_data)
{
	char			str_temp[56]	= {0};
	long			u_l_int			= (long)float_data;
	int				negative_flag	= 0;
	long			u_l_temp		= 0;
	int				int_count		= 0;
	int				i				= 0;
	long			one_int			= 0;
	double			_point_			= 0;
	int				one_int_p		= 0;
	unsigned int	point_len		= 5;
	
	ACC_COM_insert_reading();
	
	if(float_data < 0)
	{
		u_l_temp = -u_l_int;
		negative_flag = 1;
	}
	else
	{
		u_l_temp = u_l_int;
		negative_flag = 0;
	}
	
	while (u_l_temp)
	{
		ACC_COM_insert_reading();
		one_int = (long)(u_l_temp / 10) * 10;
		one_int = u_l_temp - one_int;
		str_temp[int_count++] = one_int + 0x30;
		u_l_temp = u_l_temp / 10;
	}
	
	if(int_count == 0)
	{
		str_temp[int_count++] = '0';
	}
	if(negative_flag) 
	{
		str_temp[int_count ++] = '-';
	}
	
	i = int_count;
	while (i)
	{
		ACC_COM_insert_reading();
		str_save[int_count-i] = str_temp[i-1];
		--i;
	}
	str_save[int_count++] = '.';

	_point_ = float_data - (double)u_l_int;
	_point_ = (negative_flag ? -_point_ : _point_);
	while (point_len)
	{
		ACC_COM_insert_reading();
		_point_ *= 10;
		one_int_p = _point_;
		if((one_int_p > 9) || (one_int_p < 0)) 
			one_int_p = 0;
		str_save[int_count++] = one_int_p + 0x30;
		_point_ -= one_int_p;
		--point_len;
	}
	str_save[int_count++] = 0;
	return str_save;
}

/******************************************************************************
end
******************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -