原创 SI4133驱动

2008-12-8 00:08 2324 4 6 分类: MCU/ 嵌入式

/*********单个SI4133测试,原应用于某上下变频器器实例*******
Project : SI4133上变频器实例
Version :
Date    : 2006-9-27
Author  : TeleSys Embedded               
Company : FastmanSoft Inc.               
Comments:
Chip type           : ATmega16L
Program type        : Application
Clock frequency     : 10.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 256
*****************************************************/


#include <mega16.h>
#include <stdio.h>
#include <delay.h>
#include "eeprom.h"


#include "si4133.h"
#include "si413x.c"


void put_com(void);
void ChushihuaData(char data_Add_basic);
void command(void);
void Tx_ata(void);


char com_ok;
char Add_Bit,Data_Bit,Add_Data;
char Add_B,Data_B,AD_B;
long data_temp;
char SI_CE;


char zt[4] ={0x55,0xaa,0x55,0xaa};


#define Addr_Bit_basic 0x10
#define Addr_data_shuanxu 0x12
#define data_Bit_basic 0x15
#define data_Add_basic 0x20


 


#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7


#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)


// USART Receiver buffer
#define RX_BUFFER_SIZE 12
char rx_buffer[RX_BUFFER_SIZE];


#if RX_BUFFER_SIZE<256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif


// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;


// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
   {
   
   rx_buffer[rx_wr_index]=data;
   if((rx_wr_index < 4)&&(zt[rx_wr_index] != rx_buffer[rx_wr_index]))
     {
        rx_counter=0;  
     }
//   putchar(rx_buffer[rx_wr_index]);
   if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
   if (++rx_counter == RX_BUFFER_SIZE)
      {
       rx_counter=0;
       rx_buffer_overflow=1;
       com_ok = 1;
     
      };
   };
}


#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif


// USART Transmitter buffer
#define TX_BUFFER_SIZE 12
char tx_buffer[TX_BUFFER_SIZE];


#if TX_BUFFER_SIZE<256
unsigned char tx_wr_index,tx_rd_index,tx_counter;
#else
unsigned int tx_wr_index,tx_rd_index,tx_counter;
#endif


// USART Transmitter interrupt service routine
interrupt [USART_TXC] void usart_tx_isr(void)
{
if (tx_counter)
   {
   --tx_counter;
   UDR="tx"_buffer[tx_rd_index];
   if (++tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0;
   };
}


#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART Transmitter buffer
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while (tx_counter == TX_BUFFER_SIZE);
#asm("cli")
if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0))
   {
   tx_buffer[tx_wr_index]=c;
   if (++tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0;
   ++tx_counter;
   }
else
   UDR="c";
#asm("sei")
}
#pragma used-
#endif


// Standard Input/Output functions



// Declare your global variables here


void main(void)
{
// Declare your local variables here


// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;


// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=Out Func1=Out Func0=Out
// State7=T State6=T State5=T State4=T State3=0 State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0x0F;


// Port C initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In
// State7=0 State6=0 State5=0 State4=0 State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0xF0;


// Port D initialization
// Func7=In Func6=Out Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=In
// State7=T State6=0 State5=T State4=T State3=T State2=T State1=1 State0=T
PORTD=0x02;
DDRD=0x42;


// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top="FFh"
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;


// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top="FFFFh"
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;


// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top="FFh"
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;


// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;


// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;



// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0xD8;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x40;


// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;


// Global enable interrupts
//#asm("sei")
Power = 0;   //打开PLL电源
SI_sclk  = 1;
SI_data  = 1;
SI_CE1   = 1;
SI_CE2   = 1;
EEROR_LED  = 1;
BoaJing_LED  = 1;
DaiJi_LED    = 1;
GongZhuo_LED = 1;
com_ok =0;
Add_B = Data_B = AD_B = data_temp =0;


SI_CE = 0x02;
////初始化地址占用的Bit位数
Add_Bit = EEPROM_read(Addr_Bit_basic);
if(Add_Bit > 0x8)
  {
    Add_B = 1;
    Add_Bit = 4;
    EEPROM_write(Addr_Bit_basic,Add_Bit); 
  }
 
////初始化数据占用的Bit位数
Data_Bit = EEPROM_read(data_Bit_basic);
if(Data_Bit > (32-Add_Bit))
  {
    Data_B = 1;
    Data_Bit = 17; 
    EEPROM_write(data_Bit_basic,Data_Bit);
  }
  
////初始化数据发送顺序,0:先发地址D0Bit再发数据D0Bit;1:先发地址DXBit再发数据DXBit;2:先发数据D0Bit再发地址D0Bit;3:先发数据的DXBit再发地址的DXBit;
Add_Data  = EEPROM_read(Addr_data_shuanxu);
if(Add_Data > 3)
   {
     AD_B = 1;
     Add_Data = 0; 
     EEPROM_write(Addr_data_shuanxu,Add_Data);
   }
if((Add_B!=0)||(Data_B!=0)||(AD_B!=0))
  {
    ChushihuaData(data_Add_basic);
    Add_B = Data_B = AD_B = 0;
  }
else
  {
   Tx_ata();  
 
  }


#asm("sei")  
  
while (1)
      {
        if(com_ok == 1)
          {
            put_com();    ////输出接收到的数据 
            command();
          }
    
     //// SI4133_Tx_data(0x0010aab0,1);


      };
}



void put_com(void)
    { char temp;
      for(temp = 0;temp<12;temp++)
         {
           putchar(rx_buffer[temp]); 
        
         }
     }


void command(void)
     {
       char temp,nchifang;
       switch(rx_buffer[4])
            {
              case 0:
                  { //
                   printf("\n SiChuang XiJiang JiRuijiang."); 
                   printf("\n Si4133 DuDong ChengXu");
                   printf("\n Ver:0.0.1");
                   printf("\n 2006.10.8");
                   printf("\n");
                   break;
                  }
              case 1:
                  { //
                    
                   break;
                  }
             case 2: //设置发送数据的地址有效位,默认为4Bit,
                  { 
                   
                    break;
                  }
             case 5: //发送数据并保存。
                  { 
                         switch(temp)
                         {
                          case 1:
                             { //
                  
                              break;
                             }
                         case 2:
                             { //
                  
                              break;
                             }
                         case 3:
                             { //
                  
                              break;
                             }
                         default: //
                             {
                              data_temp =0;
                              for(temp=0;temp<4;temp++)     ////合成控制数据
                                 {
                                   data_temp |= rx_buffer[6+temp];
                                   data_temp = data_temp<<8; 
                                 }
                              data_temp = data_temp<<Add_Bit;     ////准备叠加地址
                              nchifang = 0;
                              for(temp=0;temp<4;temp++)
                                   nchifang |= 0x01 << 1; 
                              data_temp |= rx_buffer[5]&nchifang; ////合成发送数据
                              SI4133_Tx_data(data_temp,SI_CE);
                              ////保存
                              EEPROM_write4(data_Add_basic+Add_Bit*4,data_temp);
                              break; 
                             }   
                        break;
                       }
                default: //
                     {
                       break; 
                     }  
                    break;
                  }
            case 6: //发送数据,
                  { 
                   
                    switch(temp)
                         {
                          case 1:
                             { //
                  
                              break;
                             }
                         case 2:
                             { //
                  
                              break;
                             }
                         case 3:
                             { //
                  
                              break;
                             }
                         default: //
                             {
                              data_temp =0;
                              for(temp=0;temp<4;temp++)     ////合成控制数据
                                 {
                                   data_temp |= rx_buffer[6+temp];
                                   data_temp = data_temp<<8; 
                                 }
                              data_temp = data_temp<<Add_Bit;     ////准备叠加地址
                              nchifang = 0;
                              for(temp=0;temp<4;temp++)
                                   nchifang |= 0x01 << 1; 
                              data_temp |= rx_buffer[5]&nchifang; ////合成发送数据
                              SI4133_Tx_data(data_temp,SI_CE);
                             
                              break; 
                             } 
                           }
                            break;
                         } 
                     
                       }
//default://
//                     {
//                       break; 
//                     }   
        //        }
           }
 
void ChushihuaData(char dataAddbasic)
      {
       char temp;
       for(temp=0;temp<32;temp++)
          {
          EEPROM_write4(dataAddbasic+temp*4,0);
          }
      }
void Tx_ata(void)
      {
       long telmp;
       char temp;
       for(temp=0;temp<32;temp++)
          {
           telmp = EEPROM_read4(data_Add_basic+temp*4);
           if(temp <16)
             SI4133_Tx_data(telmp,0);
           else
             SI4133_Tx_data(telmp,1);
          }
      }


https://static.assets-stash.eet-china.com/album/old-resources/2008/12/7/93b9e20f-a602-4e7f-bb90-e3b5ffd026ca.rar

PARTNER CONTENT

文章评论2条评论)

登录后参与讨论

用户43378 2010-1-30 19:01

dddddddd

用户184613 2008-12-8 16:16

ddddddddddddddddddddddddd
相关推荐阅读
用户389572 2010-12-12 21:02
2010工商管理-资源与营运管理机考答案
2010工商管理-资源与营运管理机考答案...
用户389572 2010-04-27 21:21
千古望江 百世芙蓉
横批:千古望江 百世芙蓉 上联:望江楼,望江流,望江楼上望江流,江楼千古,江流千古。 下联:芙蓉城,芙蓉盛,芙蓉城下扶蓉盛,蓉城百世,蓉盛百世。高人请指正或对下联。...
用户389572 2009-11-27 12:50
川电09秋工商专全部答案
川电09秋工商专全部答案(地域文化外)RAR解压后用金山WPS2009打开。https://static.assets-stash.eet-china.com/album/old-resources/...
用户389572 2009-10-09 01:08
寒热交错型H1N1流感中药方
特别说明:1、本方仅使用于寒热错杂型H1N1流感预防和治疗中药方,其他类型的H1N1不能应用!切记切记,人命关天,不得儿戏!2、鉴于西药的副作用:耐药性、病毒变异、治标不治本等,治病首选中药,西药次之...
用户389572 2009-10-09 00:37
药名四季歌
药名四季歌春春风和煦满常山,芍药天麻及牡丹;远志去寻使君子,当归何必问泽兰。夏端阳半夏五月天,菖蒲制酒乐半年;庭前娇女红娘子,笑与槟榔同采莲。秋秋菊开花遍地黄,一日雨露一回香;牧童去取国公酒,醉到天南...
用户389572 2009-10-09 00:34
中药名歇后语
中药名歇后语我国中草药品种繁多,使用历史悠久,中草药知识在民间传播甚广,不少已为人们用作歇后语,读起来十分形象、生动。如下:  口吃青果——先苦后甜。  花椒大料——两味。  水银泻地——无孔不入。 ...
EE直播间
更多
我要评论
2
4
关闭 站长推荐上一条 /3 下一条