当前位置: 代码迷 >> 单片机 >> 哪位高手有atmega8与tlc2543 的转换程序
  详细解决方案

哪位高手有atmega8与tlc2543 的转换程序

热度:100   发布时间:2016-04-28 16:01:36.0
谁有atmega8与tlc2543 的转换程序
谁有atmega8与tlc2543 的转换程序
OUT PC0
CS PC1
IN PC2
CLK PC3

菜鸟不懂,请高手指教!!!

------解决方案--------------------
#include <iom8v.h>
#include <macros.h>
#include <stdio.h>
#include <math.h>
void port_init(void);//端口初始化函数
void uart_init(void);//M8串口初始化函数
void init_devices(void);//整个设备初始化函数
void delay_1ms(void);//延时1ms程序
void delay(unsigned int n);//延时n毫秒程序
unsigned int p10x(unsigned int i);//自制10的N次方函数
void SPI_MasterInit(void);//SPI端口初始化函数
unsigned char SPI_TranByte(unsigned char );//SPI传送命令字以及接收转换结果的函数
void Format_tran(unsigned int data1,unsigned int data2);// 把转换结果从整型数转化为相应字 // 符串并从串口发到PC的函数

void main()
{
 unsigned int addata = 0 ;//接收转换结果的变量
 unsigned char adcl = 0 ;//转换结果高字节
 unsigned char adch = 0 ;//转换结果低字节
 unsigned char CtrlByte = 0;//定义控制字
 OSCCAL = 0xA3;
 init_devices();
 UCSRA &= 0B00100000;

 PORTB |=0B00000100;//片选禁止
 PORTB &=0B11111011; //片选使能
 delay_1ms();
 while(1)
{ addata = 0 ;
adcl = 0 ;
adch = 0 ;
CtrlByte = 0B00001100;//选择外部模拟输入通道0,16位分辨率,高字节先进入
adch = SPI_TranByte(CtrlByte);
adcl = SPI_TranByte(CtrlByte);
addata |= adch;
addata <<= 8;
addata |= adcl ;
Format_tran(addata,0xffff);
CtrlByte = 0B00011100;//选择外部模拟输入通道1,16位分辨率,高字节先进入
adch = SPI_TranByte(CtrlByte);
adcl = SPI_TranByte(CtrlByte);
addata |= adch;
addata <<= 8;
addata |= adcl ;
Format_tran(addata,0xffff);
}
}
 
 
void port_init(void)
{
 PORTB = 0xFF;
 DDRB = 0x00;
 PORTC = 0x00; //m103 output only
 DDRC = 0x00;
 PORTD = 0xFF;
 DDRD = 0x00;
}

//UART0 initialisation
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart_init(void)
{
 UCSRB = 0x00; //disable while setting baud rate
 UCSRA = 0x00;
 UCSRC = 0x86;
 UBRRL = 0x33; //set baud rate lo
 UBRRH = 0x00; //set baud rate hi
 UCSRB = 0x08;
}

void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 uart_init();
 SPI_MasterInit();
 MCUCR = 0x00;
 GICR = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 //SPI_MasterInit();
 SEI(); //re-enable interrupts
 //all peripherals are now initialised
}

void delay_1ms(void)
{unsigned int i;
 for(i=1;i<(unsigned int)(8000000*143-2);i++)
;
}

void delay(unsigned int n)
{unsigned int i=0;
 for(i=0;i<n;i++)
 delay_1ms();
}


unsigned int p10x(unsigned int i)

unsigned int j=0;
unsigned int result=1;
for(j=0;j<i;j++)
result = result * 10;
return result;
}

void SPI_MasterInit(void)
{
 DDRB = 0B11101111;
 SPCR = 0x50;
}

unsigned char SPI_TranByte(unsigned char bData)
{unsigned char clear = 0;
 SPDR = bData;
 while(!(SPSR & 0x80));
 clear = SPSR;
 return SPDR;
}

void Format_tran(unsigned int data1,unsigned int data2)
{ int i=0;
unsigned char data[5] = "785j" ;
data1 =(unsigned int)(((unsigned long)((unsigned long)data1*5000))/data2);
for(i=0;i<4;i++)
{

data[i] = data1 / p10x(3-i);
data1 = data1 - (data1 / p10x(3-i)) * p10x(3-i);
  相关解决方案