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

📄 smbus.c

📁 c8051单片机使用smbus(I2C)接口的测试程序
💻 C
字号:


#include "absacc.h"
#include "intrins.h"


#include "hand_serial_all_h.h"
#include "sa5753_fun_h.h"



//从串口接收的消息包
BYTE news_data_buff[20];
idata BYTE news_len=0;//收消息时的接收长度
idata BYTE serial_status= 0 ;

extern type_of_timer m_music_delay_timer;

void recv_comm_from_serial()
{
	BYTE idata l_in_char;

	if(!if_empty_com_0())
	{
		l_in_char=getbyte_com_0();
	}
	else 
		return;
		
	switch(serial_status) 
	{
		case 0:
			if(l_in_char==0x7e)
			{
				serial_status=1;
				news_len=0;
			}
		break;
		
		case 1:
			serial_status=2;
			news_data_buff[news_len++]=l_in_char;
		break;

		case 2:
			serial_status=3;
			news_data_buff[news_len++]=l_in_char;
		break;
		
		case 	3:
			news_data_buff[news_len++]=l_in_char;
			if(news_len==news_data_buff[0])//收齐一包
			{
				serial_status = 0;	
				switch(news_data_buff[1])
				{
					case 1:
					{
						SM_Send(SA5753_ADDR,news_data_buff[2],news_data_buff[3]);
						puthex_com_0(news_data_buff[2]);
						puthex_com_0(news_data_buff[3]);
					}
					break;
					
					case 2:
					{
						sa5753_test_DTMF();
						putstring_com_0(" test_DTMF ");
					}
					break;
					
					case 3:
					{
						sa5753_change_DTMF(news_data_buff[2]);
						puthex_com_0(news_data_buff[2]);
					}
					break;
					
					case 4:
						sa5753_start_music();
						putstring_com_0("start music");
					break;
					
					case 5:
						sa5753_stop_music();
						putstring_com_0("stop music");
					break;
					
					default:
					break;
				}
			}
	}	
}	


void SYSCLK_Init (void)
{
  int i;                              // delay counter
  
  OSCXCN = 0x27;                      // start external oscillator with
//	OSCXCN = 0x67;                      // start external oscillator with
  for (i=0; i < 256; i++) ;           // XTLVLD blanking interval (>1ms)
//   while (!(OSCXCN & 0x80)) ;          // Wait for crystal osc. to settle
  OSCICN = 0x08;                      // select external oscillator as SYSCLK
                                       // source and enable missing clock
                                       // detector
 // CKCON =0x00;					//定义4个定时器的时钟滴答为系统钟的12分频   p187                                 
 CKCON=0x78;
}

//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports
//
void PORT_Init (void)
{
  XBR0    = 0x05;                     // Enable UART0 and smbus
  XBR1    = 0x00;


  XBR2    = 0x40;                     // Enable crossbar and weak pull-ups
  P0MDOUT |= 0x01;                    // enable TX0 as a push-pull output
  P1MDOUT |= 0x00;                    // enable P1 as push-pull output
  P2MDOUT |= 0x00;
  P3MDOUT |= 0x00;
	
	EMI0CF=0x23;						//只使用内部XRAM P53 	
}

void timer_init(void)
{
	TMOD|=0x01;
	TH0= (65536-TIMER_CONSTANT)/256;
	TL0= (65536-TIMER_CONSTANT)%256;
	ET0=1;
	TR0=1;
}

void timer0(void) interrupt  1 using 2
{
	TH0= (65536-TIMER_CONSTANT)/256;
	TL0= (65536-TIMER_CONSTANT)%256;
	if(	m_music_delay_timer.active)
	{
		m_music_delay_timer.timer_value--;
		if(m_music_delay_timer.timer_value==0)
		{
			m_music_delay_timer.active=0;
			m_music_delay_timer.timer_off=1;
		}
	}

}


void main(void)
{
	BYTE idata l_5753_reg_addr;
	int i;
	
	WDTCN = 0xde;									// disable watchdog timer
	WDTCN = 0xad;
	
	SYSCLK_Init ();                     // initialize oscillator
	PORT_Init ();                       // initialize crossbar and GPIO

	timer_init();
	serial_init();	

	smbus_init();
	
	EA = 1;											// Global interrupt enable

	putstring_com_0("com0,9600,n,8,1");

	for(i=0;i<2000;i++);

	sa5753_init();

	while(1)
	{
		recv_comm_from_serial();		
		
		sa5753_play_music_proc();
		
	}
}

⌨️ 快捷键说明

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