AVR mega16 SPI dual-machine communication example that has been debugged

Publisher:小星星龙猫Latest update time:2016-08-16 Source: eefocusKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//ICC-AVR application builder : 2007-7-18 13:01:11

// Target : M16
// Crystal: 7.3728Mhz
// 作者:古欣
// AVR与虚拟仪器 [url]http://www.avrvi.com[/url]
// 功能:SPI主机模式,循环发送从1~255

#include
#include

void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}

//SPI initialize
// clock rate: 57599hz
void spi_init(void)
{
PORTB |= (1< DDRB |= (1< SPCR = 0x73; //setup SPI
SPSR = 0x00; //setup SPI
}

//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
spi_init();

MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}

void SPI_MasterTransmit(char cData)
{
PORTB &=~ (1< SPCR |= (1< /* 启动数据传输 */
SPDR = cData;
/* 等待传输结束 */
while(!(SPSR & (1< ;
PORTB |= (1< }

void Delay(void) //延时,没有详细计算
{
unsigned int i,j;
for(i=1000;i>0;i--)
{
for(j=200;j>0;j--)
;
}
}

void main(void)
{
unsigned char i=0;
init_devices();
while(1)
{
for(i=255;i>0;i--)
{
SPI_MasterTransmit(i);
Delay();
}
}
}

//ICC-AVR application builder : 2007-7-18 12:56:10
// Target : M16
// Crystal: 7.3728Mhz
// 作者:古欣
// AVR与虚拟仪器 [url]http://www.avrvi.com[/url]
// 功能:从机模式,中断方式接收,并在LED上显示

#include
#include

void port_init(void)
{
PORTA = 0x00;
DDRA = 0xFF;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}

//SPI initialize
// clock rate: 57599hz
void spi_init(void)
{
SPCR = 0xE3; //setup SPI
SPSR = 0x00; //setup SPI
}

#pragma interrupt_handler spi_stc_isr:11
void spi_stc_isr(void)
{
//byte in SPDR has been sent/received
PORTA = SPDR;
}

//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
spi_init();

MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}

void main(void)
{
init_devices();
DDRB|=(1< while(1)
;//等待中断
}

Keywords:AVR Reference address:AVR mega16 SPI dual-machine communication example that has been debugged

Previous article:AVR's SD card basic reading and writing procedures (I)
Next article:AVR IO port characteristics and applications

Recommended ReadingLatest update time:2024-11-16 15:04

AVR control 4-wire touch screen program
The four-wire resistive touch screen is the most widely used and popular type in the resistive family. Its structure consists of a lower line (glass or film material) conductive ITO layer and an upper line (film The upper and lower lines are open circuit when there is no pressure on the touch screen surface. Once pres
[Microcontroller]
AVR 1-99 seconds 1602 start and stop display
#include #include intrinsics.h #define uchar unsigned char #define uint unsigned int uint sec=0; //second variable uint cnt=0; //count variable uint on=0; //start/stop status variable uchar str1 ="The time: sec"; //All strings here have a certain length to eliminate display abnormalities caused by the pointe
[Microcontroller]
Reading and Writing EEPROM of AVR Microcontroller
AVR storage type  does not need external plug-in for easy use     1. FLASH          stores program code     2. EEPROM        runs data that is not lost when power is off and  stores    paging data      3.            Temporary data of SRAM operation EEPROM: read and write in bytes  through dedicated instruction
[Microcontroller]
Temperature sensor ds18b20-AVR program code
#include   #include      // Alphanumeric LCD Module functions #asm    .equ __lcd_port=0x18 ;PORTB #endasm #include      #define  uchar unsigned char  #define  uint unsigned int  #define  BIT(x) 1 (x) #define DQ_IN DDRC&=~BIT(2)     #define DQ_OUT DDRC|=BIT(2) #define DQ_SET PORTC|=BIT(2) #define DQ_CLR PORTC&=~BIT(2)
[Microcontroller]
AVR microcontroller - (three), ATMEGA16 drive 16 * 2 dot matrix character LCD module - 01
3. ATMEGA16 drives 16*2 dot matrix character LCD 3. (01) ATMEGA16 drives 16*2 dot matrix character LCD This article is just a simple driver to make 1602 display, and does not use the part of reading data and reading status, nor does it use read and write detection (if you are interested, you can still write it here)
[Microcontroller]
AVR microcontroller - (three), ATMEGA16 drive 16 * 2 dot matrix character LCD module - 01
ADC Button Design Techniques Based on AVR Microcontroller
1 Introduction In the field of single-chip microcomputer applications, the ways and methods of human-computer interaction are flexible and diverse, but keyboard input is still the most common and important method. The usual ways to implement keyboard interface are: direct input, matrix structure, A/D mode, keyb
[Microcontroller]
AVR128 MCU minimum system serial port, external interrupt, marquee, uCOS-II transplantation
Experiment name: Serial communication experiment Function description: The computer sends data to the development board through the serial port, and the development board sends the data back as is. Experiment purpose: Learn serial communication Experiment description: MCU--M128           internal 8M crystal oscillator
[Microcontroller]
Implementation of Coding and Decoding of Load Cylinder Multi-channel Remote Control Switch Based on AVR Single Chip Microcomputer
The load cylinder is an important tooling equipment necessary for the load test of the core generator performance of the ship power. Through the load test, the long-term performance stability of the generator (static characteristics) and the adaptability to sudden load addition and unloading (dynamic characteristics
[Microcontroller]
Implementation of Coding and Decoding of Load Cylinder Multi-channel Remote Control Switch Based on AVR Single Chip Microcomputer
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号