ATmega16 单片机SPI的问题
时间:10-02
整理:3721RD
点击:
大神们,帮看看这个程序哪里有问题!为什么烧进单片机中两个机器却不能通信!
Master ;
硬件电路是将单片机的PB4~7 相互连接的!找了好久百度,都没有结果!
Master ;
- #include <mega16.h>
- #include <delay.h>
- void PortInit(void){
- DDRB = 0XBF;
- PORTB = 0X00;
- }
- void SPI_Init(void){
- SPCR = 0x51;
- }
- void SPI_Send(unsigned char ch){
- SPDR = ch;
- while(!(SPSR & (1<<SPIF)));
- }
- void MCU_Init(void){
- PortInit();
- SPI_Init();
- }
- void main(void){
- MCU_Init();
- while(1){
- SPI_Send('C');
- delay_ms(1000);
- }
- }
- #include <mega16.h>
- #include <stdio.h>
- void PortInit(void){
- DDRB = 0X4F;
- PORTB = 0X0f;
- DDRD = 0X02;
- PORTD = 0XFF;
- }
- void uart_init(void){
- UCSRB = 0X00; //禁止发送与接收
- UCSRA = 0X82; //倍速
- UCSRC = 0X06; //8位数据位
- UBRRL = 0X67; //波特率9600
- UBRRH = 0X00;
- UCSRB = 0X98;
- }
- void SPI_Init(void){
- SPCR = 0X41;
- //SPSR = 0x41;
- }
- unsigned char SPI_Rec_Data(void){
- unsigned char tmp = 0;
- while(!(SPSR&(1<<SPIF)));
- tmp = SPDR;
- return tmp;
- }
- void MCU_Init(void){
- PortInit();
- uart_init();
- SPI_Init();
- }
- void main(){
- unsigned char tmp = 0;
- MCU_Init();
- while(1){
- tmp = SPI_Rec_Data();
- SPSR &= ~(1<<SPIF);
- printf("%c", tmp);
- PORTB = 0X00;
- }
- }
硬件电路是将单片机的PB4~7 相互连接的!找了好久百度,都没有结果!