基于atmega16的遥控车设计
四年级 记叙文 8459字 96人浏览 星魂入心梦

/*

* yaokongche_che.c

*

* Created: 2015/9/7 20:56:55

* Author: LOVE

*/

/************************************************************************/ /* 常用头文件及宏定义 */ /************************************************************************/

#include <avr/io.h>

#include <util/delay.h> //延时文件

#include <avr/sleep.h> //睡眠文件

#include <avr/interrupt.h> //中断文件

#include <avr/eeprom.h> //eeprom

#define BIT(x) (1<<(x))

#define uchar unsigned char

#define uint unsigned int

// PC 6 绿色 PC 7 红色 PB 2 蓝色

#define HONG_H PORTC|=BIT(7)

#define HONG_L PORTC&=~BIT(7)

#define LV_H PORTC|=BIT(6)

#define LV_L PORTC&=~BIT(6)

#define LAN_H PORTB|=BIT(2)

#define LAN_L PORTB&=~BIT(2)

// PB 4 SI PB 5 RCK PB6 SCK

#define SI_H PORTB|=BIT(4)

#define SI_L PORTB&=~BIT(4)

#define RCK PORTB|=BIT(5),PORTB&=~BIT(5)

#define SCK_595 PORTB|=BIT(6),PORTB&=~BIT(6)

// PD 4 C 1 PD 5 C2 PD 6 C3

#define C1_H PORTD|=BIT(4)

#define C1_L PORTD&=~BIT(4)

#define C2_H PORTD|=BIT(5)

#define C2_L PORTD&=~BIT(5)

#define C3_H PORTD|=BIT(6)

#define C3_L PORTD&=~BIT(6)

/************************************************************************/ /* NRF24L01 */ /************************************************************************/

// PA0 CE PA1 CSN PA2 SCK PA3 MOSI PA4 MISO PA5 IQR #define DDR_NRF2401 DDRC

#define PORT_NRF2401 PORTC

#define PIN_NRF2401 PINC

#define CE_H PORT_NRF2401|=BIT(0)

#define CE_L PORT_NRF2401&=~BIT(0)

#define CSN_H PORT_NRF2401|=BIT(1)

#define CSN_L PORT_NRF2401&=~BIT(1)

#define SCK PORT_NRF2401|=BIT(2),PORT_NRF2401&=~BIT(2) #define MOSI_H PORT_NRF2401|=BIT(3)

#define MOSI_L PORT_NRF2401&=~BIT(3)

#define MISO_PIN (PIN_NRF2401&BIT(4))

#define IQR_PIN (PIN_NRF2401&BIT(5))

char a[3]={0};

char read_peizhi(char dizhi)

{

CSN_L;

char i;

for (i=0;i<8;i++)

{

if (dizhi&BIT(7-i))

{

MOSI_H;

}

else

{

MOSI_L;

}

SCK;

}

dizhi=0;

for (i=0;i<8;i++)

{

_delay_us(10);

if (MISO_PIN)

{

dizhi+=BIT(7-i);

}

SCK;

}

CSN_H;

return dizhi;

}

void write_peizhi(char dizhi,char shuju) {

char i;

CSN_L;

dizhi+=BIT(5);

for (i=0;i<8;i++)

{

if (dizhi&BIT(7-i))

{

MOSI_H;

}

else

{

MOSI_L;

}

SCK;

}

for (i=0;i<8;i++)

{

if (shuju&BIT(7-i)) {

MOSI_H;

}

else

{

MOSI_L;

}

SCK;

}

CSN_H;

}

void read_shuju(uchar n)

{

uchar i,j;

CSN_L;

for (i=0;i<8;i++)

{

if (0x61&BIT(7-i)) {

MOSI_H;

}

else

{

MOSI_L; }

SCK;

}

for (j=0;j<n;j++)

{

a[j]=0;

for (i=0;i<8;i++) {

_delay_us(10); if (MISO_PIN) {

a[j]+=BIT(7-i); }

SCK;

}

}

CSN_H;

}

void write_shuju(char shuju) {

char i;

CSN_L;

for (i=0;i<8;i++)

{

if (0xa0&BIT(7-i)) {

MOSI_H; }

else

{

MOSI_L; }

SCK;

}

for (i=0;i<8;i++)

{

if (shuju&BIT(7-i)) {

MOSI_H; }

else

{

MOSI_L;

}

SCK;

}

CSN_H;

}

void qingchu_tx() {

char i;

CSN_L;

for (i=0;i<8;i++) {

if (0xe1&BIT(7-i)) {

MOSI_H; }

else

{

MOSI_L; }

SCK;

}

CSN_H;

}

void qingchu_rx() {

char i;

CSN_L;

for (i=0;i<8;i++) {

if (0xe2&BIT(7-i)) {

MOSI_H; }

else

{

MOSI_L; }

SCK;

}

CSN_H;

}

char read_zhuangtai() {

CSN_L;

char i,zhuangtai=0;

MOSI_H;

for (i=0;i<8;i++)

{

SCK;

_delay_us(40);

if (MISO_PIN)

{

zhuangtai+=BIT(7-i);

}

}

CSN_H;

return zhuangtai;

}

void read_dizhi(char dizhi,char *shuju,char zijie) {

CSN_L;

char i,j;

for (i=0;i<8;i++)

{

if (dizhi&BIT(7-i))

{

MOSI_H;

}

else

{

MOSI_L;

}

SCK;

}

shuju+=zijie;

for (j=0;j<zijie;j++)

{

shuju--;

*shuju=0;

for (i=0;i<8;i++)

{

_delay_us(10);

if (MISO_PIN)

{

*shuju+=BIT(7-i); }

SCK;

}

}

CSN_H;

}

void write_dizhi(char dizhi,char *shuju,char zijie) {

char i,j;

CSN_L;

dizhi+=BIT(5);

for (i=0;i<8;i++)

{

if (dizhi&BIT(7-i))

{

MOSI_H;

}

else

{

MOSI_L;

}

SCK;

}

shuju+=zijie;

for (j=0;j<zijie;j++)

{

shuju--;

for (i=0;i<8;i++)

{

if (*shuju&BIT(7-i))

{

MOSI_H;

}

else

{

MOSI_L;

}

SCK;

}

}

CSN_H;

}

void chushihua_nrf2401()

{

DDR_NRF2401|=BIT(0)|BIT(1)|BIT(2)|BIT(3); DDR_NRF2401&=~BIT(4);

PORT_NRF2401|=BIT(4);

CSN_H;

CE_L;

char dizhi[5]={'l','o','v','e',0x01};

qingchu_rx();

qingchu_tx();

write_peizhi(0x07,0xff);

write_peizhi(0x00,0x0b); write_peizhi(0x02,0x01);

write_dizhi(0x0a,dizhi,5);//接收地址love01

dizhi[4]=0x02;

write_dizhi(0x10,dizhi,5);//发送地址love02

write_peizhi(0x11,0x03);//接收数据宽度3

CE_H;

}

/************************************************************************/ /* 74_595 */ /************************************************************************/

void chushihua_595()

{

uchar i;

SI_L;

for (i=0;i<8;i++)

{

SCK_595;

}

RCK;

}

void write_74hc595(uchar shuju)

{

uchar i;

for (i=0;i<8;i++)

{

if (shuju&BIT(7-i))

{

SI_H;

}

else

{

SI_L;

}

SCK_595;

}

RCK;

}

uchar ting=0;

uchar shuma[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//数码0~9

/************************************************************************/ /* ADC */ /************************************************************************/ long dianya=0;

uint adczhi=700;

void chushihua_adc()

{

DDRA&=~BIT(1);

ADMUX=0x41;

ADCSRA|=BIT(ADEN)|BIT(ADSC)|BIT(ADPS0);

while(!(ADCSRA&BIT(ADIF)));//等待转换结束

dianya=ADCL+(ADCH<<8);//第一次舍去

ADCSRA|=BIT(ADIF);//清标志

ADCSRA|=BIT(ADSC);

}

/************************************************************************/ /* PWM */ /************************************************************************/

void chushihua_pwm()

{

DDRB|=BIT(3);

DDRD|=BIT(7);

OCR0=98;//15~27 21

OCR2=0;

cli();

TCCR0|=BIT(WGM00)|BIT(COM01)|BIT(CS01)|BIT(CS00);

TIMSK|=BIT(TOIE1);

TCCR1B|=BIT(CS10);

TCCR2|=BIT(WGM20)|BIT(COM20)|BIT(COM21)|BIT(CS21);

sei();

}

ISR(TIMER1_OVF_vect)

{

GICR&=~BIT(INT0);

static uchar k=1,n=0,j=0;

static uint num=0; if(ADCSRA&BIT(ADIF))//等待转换结束 { if (j==50) { dianya=num/50; num=0; j=0; dianya=dianya*1000/1023; if (dianya-5>adczhi) { adczhi=dianya; } else { if (dianya+5<adczhi) { adczhi=dianya; } } } else { j++; num+=ADCL+(ADCH<<8); } ADCSRA|=BIT(ADIF);//清标志 } TCNT1=10000; switch(k) { case 1: { C2_L; C3_L; if (adczhi>99) { write_74hc595(shuma[adczhi/100%10]|BIT(7)); C1_H; } }break;

case 2: {

C1_L; C3_L; if (adczhi>9) { write_74hc595(shuma[adczhi/10%10]); C2_H; } }break; case 3: { C1_L; C2_L; C3_H; write_74hc595(shuma[adczhi%10]); }break; default:; } if (k==3) { k=1; } else { k++; } if (adczhi>600) { ADCSRA|=BIT(ADSC); n=0; ting++; if (ting>5) { OCR0=98; OCR2=0; LV_L; LAN_H; HONG_L; } } else { if (n==255)

{ LV_L;

LAN_L;

HONG_H;

C1_L;

C2_L;

C3_H;

write_74hc595(shuma[0]);

OCR0=98;

OCR2=0;

MCUCR&=~BIT(ISC01);

}

else

{

ADCSRA|=BIT(ADSC);

}

n++;

}

GICR|=BIT(INT0);

}

/************************************************************************/ /* 电机 */ /************************************************************************/

#define DDR_DIANJI DDRB

#define PORT_DIANJI PORTB

void chushihua_dianji()

{

PORT_DIANJI&=~(BIT(0)|BIT(1));

DDR_DIANJI|=BIT(0)|BIT(1);

}

/************************************************************************/ /* 中断 */ /************************************************************************/

void chushihua_int0()

{

MCUCR|=BIT(ISC01);

GICR|=BIT(INT0);

DDRD&=~BIT(2);

PORTD|=BIT(2);

sei();

}

ISR(INT0_vect)

{

LV_H;

LAN_L;

uchar zhuangtai;

zhuangtai=read_peizhi(0x07); ting=0;

if (zhuangtai&BIT(6))

{

read_shuju(3);

OCR0=a[0];

if (a[2]&BIT(4))

{

PORTC|=BIT(5); }

else

{

PORTC&=~BIT(5); }

if (a[2]&BIT(5))

{

OCR2=a[1];

}

else

{

OCR2=0;

}

if (a[2]&BIT(2))

{

PORT_DIANJI|=BIT(0); }

else

{

PORT_DIANJI&=~BIT(0); }

if (a[2]&BIT(3))

{

PORT_DIANJI|=BIT(1); }

else

{

PORT_DIANJI&=~BIT(1); }

if ((a[2]&BIT(2))&&(a[2]&BIT(3)))

{

OCR2=0xff;

_delay_ms(10);

OCR2=0;

}

write_peizhi(0x07,BIT(6));

qingchu_rx();

}

if (zhuangtai&BIT(0))

{

write_peizhi(0x07,BIT(0));

qingchu_tx();

}

if (zhuangtai&BIT(5))

{

write_peizhi(0x07,BIT(5));

qingchu_tx();

}

}

/************************************************************************/ /* 蜂鸣器 */ /************************************************************************/

void fengmingqi()

{

uchar i;

for (i=0;i<3;i++)

{

PORTC|=BIT(5);

_delay_ms(500);

PORTC&=~BIT(5);

_delay_ms(500);

}

}

/************************************************************************/ /* 主函数 */ /************************************************************************/

int main()

{

DDRC|=0xe0; DDRB|=0x74; DDRD|=0x70; chushihua_595(); fengmingqi(); chushihua_adc(); chushihua_dianji(); chushihua_pwm(); chushihua_nrf2401(); chushihua_int0(); while(1) { } }