|
发表于 2013-3-29 14:25:02
|
显示全部楼层
/*
* atmega16_spi_tlc2543.c
*
* Created: 2013/3/29 13:22:46
* Author: K.Yao
*/
#include <avr/io.h>
#ifndef F_CPU
#define F_CPU 4000000UL
#endif
#include <util/delay.h>
#include "bitwise.h"
#define EOC SFR(PINB,0,1)
#define SS SFR(PORTB,4,1)
#define SS1 SFR(PORTB,3,1)
#define MAX7221_Enable() SS=0
#define MAX7221_Disable() SS=1
#define TLC2543_Enable() SS1=0
#define TLC2543_Disable() SS1=1
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
void Port_Init(void)
{
DDRB = 0b10111000;
}
void SPI_Init(void)
{
SPSR = 0x00;
SPCR = _BV(SPE)|_BV(MSTR)|_BV(SPR0);
}
uchar SPI_ReadAndWrite(uchar Dat)
{
SPDR = Dat;
while(!(SPSR&_BV(SPIF)));
return (SPDR);
}
void MAX7221_WriteWord(uchar Addr,uchar Dat)
{
MAX7221_Enable();
SPI_ReadAndWrite(Addr);
SPI_ReadAndWrite(Dat);
MAX7221_Disable();
}
void MAX7221_Init(void)
{
MAX7221_WriteWord(0x0A,0x07);
MAX7221_WriteWord(0x0B,0x07);
MAX7221_WriteWord(0x0C,0x01);
MAX7221_WriteWord(0x0F,0x00);
MAX7221_WriteWord(0x09,0xFF);
}
void MAX7221_Display(uchar *DispStr)
{
for(uchar i=0;i<8;i++)
{
MAX7221_WriteWord(i+1,DispStr[i]);
}
}
uint TLC2543_ReadADValue(uchar channel)
{
uint adc = 0;
channel <<= 4;
TLC2543_Disable();
_delay_ms(3);
TLC2543_Enable();
while(0==EOC);
TLC2543_Enable();
adc |= SPI_ReadAndWrite(0x0C|channel);
adc <<= 8;
adc |= SPI_ReadAndWrite(0x00);
TLC2543_Disable();
adc &= 0xFFF;
return (adc);
}
uchar DispTab[] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07};
int main(void)
{
uint adc_value = 0;
Port_Init();
_delay_us(10);
SPI_Init();
_delay_ms(10);
MAX7221_Init();
_delay_us(10);
MAX7221_Display(DispTab);
_delay_us(10);
while(1)
{
adc_value = TLC2543_ReadADValue(1);
DispTab[0] = 10;
DispTab[1] = 15;
DispTab[2] = 15;
DispTab[3] = adc_value/1000;
DispTab[4] = adc_value%1000/100;
DispTab[5] = adc_value%100/10;
DispTab[6] = adc_value%10;
DispTab[7] = 15;
_delay_ms(20);
MAX7221_Display(DispTab);
_delay_ms(20);
//TODO:: Please write your application code
}
return (0);
} |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|