|
楼主 |
发表于 2012-1-10 22:34:01
|
显示全部楼层
自定义一个PWM格式,有点像 basicom的PLUSE 函数
//ICC-AVR application builder : 2009-3-11 14:28:07
// Target : M8
// Crystal: 8.0000Mhz
#include <iom8v.h>
#include <macros.h>
#define DK 2
#define CK 3
#define BK 1
int temp;
int P;
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
/************************************
Name :小延时程序
Target :atmega8
Crystal(晶振):4M
************************************/
/*void delay_us(int time)
{ //微秒延时
do
{
time--;
}
while (time>1);
}
*/
void delay_1us(void)
{
asm("nop");
asm("nop");
asm("nop");
asm("nop");
}
void delay_us(unsigned int n)
{ //4 M时 微秒延时
unsigned int i=0;
for(i=0;i<n;i++)
delay_1us();
}
/*
void delay_us(int J)
{
int K;
J=J*5/4;
for(K=0;K<J;K++);
//1141是在8MHz晶振下,通过软件仿真反复实验得到的数值
}*/
/**********
void delay()
{
unsigned int i;//最大值65536
for (i=0;i<40000;i++)
{;}
}
***********/
/***********************************************************
用 途:串口发送接收程序
Taget :mega8
crystal :8M
介 绍:
**********************************************************/
//晶振和波特率
#define fosc 4000000
#define baud 2400
//UART初始化函数
void init_uart()
{
UCSRB=(1<<RXEN)|(1<<TXEN)|(1<<RXCIE);//允许收发,打开接收中断
//UBRRL=(fosc/16/(baud+1))%256;//设置波特率寄存器
//UBRRH=(fosc/16/(baud+1))/256;
UBRR=103;//*4M 2400 值
UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);//8位数据+1位STOP
}
//字符输出
void uart_putchar(unsigned char c)
{
while(!(UCSRA&(1<<UDRE)));//上次发送有没有完成
UDR=c;
}
//字符输入
unsigned char uart_getchar()
{
while(!(UCSRA&(1<<RXC))){;}//有没有接收到数据
return UDR;
}
//带回车的字符串输出
int uart_print(char *s)
{
while(*s)
{
uart_putchar(*s);
s++;
}
uart_putchar(0x0a);//回车换行
uart_putchar(0x0d);
return 1;
}
//不带回车换行的字符串输出
void uart_prints(char *s)
{
while(*s)
{
uart_putchar(*s);
s++;
}
}
////PWM 自定义
//pwm()
//?
/* if(i=='B')
{
DDRB|=(1<<a);
PORTB|=(1<<a);
delay_us(b);
DDRB^=(1<<a);
PORTB^=(1<<a);
delay_us(d-b);
};*/
//void dfood_pwm(unsigned int i,int a,unsigned int b,unsigned int d)
void dfood_pwm(unsigned int i,int a,unsigned int b)
{
switch(i)
{
case 1: //0x30 ASCII '0'
DDRB|=(1<<a);
PORTB|=(1<<a);
delay_us(b);
//DDRB^=(1<<a);
PORTB^=(1<<a);
//delay_us(d-b);
break;
case 2:
DDRD|=(1<<a);
PORTD|=(1<<a);
delay_us(b);
//DDRD^=(1<<a);
PORTD^=(1<<a);
//delay_us(d-b);
break;
case 3:
DDRC|=(1<<a);
PORTC|=(1<<a);
delay_us(b);
//DDRC^=(1<<a);
PORTC^=(1<<a);
//delay_us(d-b);
break;
}
}
//}
//接收中断
#pragma interrupt_handler uart_rec_int:iv_USART_RX
void uart_rec_int()
{
unsigned char i;
i=UDR;
switch(i)
{
case 'A': //0x30 ASCII '0'
temp=1;
break;
case 'W':
temp=2;
break;
case 'S':
temp=3;
break;
case 'D':
temp=4;
break;
case 'Q':
temp=5;
break;
case 'K':
if(P!=100)P-=100;
break;
case 'M':
if(P!=2000)P+=100;
break;
}
/* uart_putchar(i);
uart_putchar(0x0a);//回车换行
uart_putchar(0x0d);*/
}
//**************************************************************************
void main()
{
port_init();
init_devices();
init_uart();
P=300;
PORTD=(1<<2);//开串口
uart_print("前W,后S,左A,右B,刹车Q,快K,慢M");
while(1)
{ DDRD=0xff;//设为输出
PORTD=(0<<2);//开串口
switch(temp)
{
case 1:
DDRC=0xff;//设为输出
PORTC=0b00111111;
dfood_pwm(CK,1,P);//c口4,1角输出PWM波
dfood_pwm(CK,4,P);
delay_us(2000-P);//延时时间
break;
case 2:
DDRC=0xff;//设为输出
PORTC=0b00111111;
dfood_pwm(CK,2,P);//c口2,5角输出PWM波
dfood_pwm(CK,5,P);
delay_us(2000-P);
break;
case 3:
DDRC=0xff;//设为输出
PORTC=0b00111111;
dfood_pwm(CK,2,P);//c口4,2角输出PWM波
dfood_pwm(CK,4,P);
delay_us(2000-P);
break;
case 4:
DDRC=0xff;//设为输出
PORTC=0b00111111;
dfood_pwm(CK,1,P);//c口5,1角输出PWM波
dfood_pwm(CK,5,P);
delay_us(2000-P);
break;
case 5:
DDRC=0xff;//设为输出
PORTC=0b00001001;
break;
}
}
} |
|