|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2010-7-25 10:40:04
|
显示全部楼层
回复【楼主位】flyto
-----------------------------------------------------------------------
软件模拟的,也不行
#include<avr/io.h>
#include"delay.h"
#include"display.h"
#define CS PB4
#define DO PB6
#define DI PB5
#define CLK PB7
unsigned int SPI_mcp3204(void);
int main(void)
{
while(1)
{
unsigned char temp;
unsigned int m,n;
m = SPI_mcp3204();
n = (m*5)/4096;
temp = (unsigned char)n;
display(temp);
}
}
unsigned int SPI_mcp3204(void)
{
unsigned char j;
unsigned int i = 0;
DDRB = 0xbf;
PORTB = 0xff;
//发送启动位和SGL,D2,D1,D0
PORTB &=(~(1 << CS));
PORTB &=(~(1 << CLK));
PORTB &= (1<< DI);//1
PORTB &=(1 << CLK);
asm("nop");
// delay(10000);
PORTB &=(~(1 << CLK));
PORTB &= (1<< DI);//1
PORTB &=(1 << CLK);
asm("nop");
//delay(10000);
PORTB &=(~(1 << CLK));
PORTB &=(~(1 << DI));//0
PORTB &=(1 << CLK);
asm("nop");
//delay(10000);
PORTB &=(~(1 << CLK));
PORTB &=(~(1 << DI));//0
asm("nop");
// delay(10000);
PORTB &=(1 << CLK);
//采样周期
asm("nop");
// delay(10000);
PORTB &=(~(1 << CLK));
asm("nop");
// delay(10000);
PORTB &=(1 << CLK);
//空位
asm("nop");
// delay(10000);
PORTB &=(~(1 << CLK));
asm("nop");
//delay(10000);
PORTB &=(1 << CLK);
//读取转换结果
for(j =12;j > 0; j--)
{
asm("nop");
// delay(10000);
PORTB &=(~(1 << CLK));
asm("nop");
// delay(10000);
i = ((PINB&0x40) >> PB6);
i <<=1;
PORTB &=(1 << CLK);
}
PORTB &= (1 << CS);//拉高片选
i >>= 1;
return(i);
} |
|