#include<iom16v.h>
#include<macros.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char k=0,c=1;
unsigned char i;
#pragma data:code
flash unsigned char juchi[127]={
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126
};
//flash unsigned int juchi[147]={
//9,38,76,116,154,188,223,247,254,234,164,50,1,9,38,76,116,154,187,217,239,252,254,240,205,145,68,19,1,1,10,22,35,49,61,73,83,90,97,101,104,104,103,99,94,87,78,68,56,43,29,16,5,0,6,36,102,175,224,248,254,247,230,204,172,136,97,58,23,1,13,101,204,247,253,237,208,170,126,83,41,8,2,60,175,237,254,248,227,197,162,125,89,54,25,4,1,26,91,166,215,240,252,254,252,246,240,232,224,216,209,204,200,198,197,198,200,204,209,216,223,231,239,246,252,254,252,241,217,170,97,29,1,4,23,52,87,123,160,195,224,246,254,239,181,66,3
//};
#pragma data:data
int f=5000;//示波器上请改变此频率
// cs = Pb^0;
// clk = Pb^1;
// din = Pb^2;
// DAC0832 CS=PC^0;
// ILE=PC^1;
// XFER=PC^2;
// WR1=PC^3;
// WR2=PC^4;
void DA_Conver(unsigned char DAValue)
{
PORTB&=0B11111110;//cs=0
PORTB&=0B11111101;//clk=0;
for(i = 0; i < 8; i++)
{
if(DAValue&0B10000000) {PORTB |= 0B00000100;}
else {PORTB&=0B11111011;}
PORTB|=0B00000010;//clk = 1;
DAValue <<= 1;
PORTB&=0B11111101; //clk = 0;
}
//9位数据为0
PORTB|=0B00000010;//clk = 1;
PORTB&=0B11111101; //clk = 0;
//10位数据为0
PORTB|=0B00000010;//clk = 1;
PORTB&=0B11111101; //clk = 0;
//第一个填充字节
PORTB|=0B00000010;//clk = 1;
PORTB&=0B11111101; //clk = 0;
//第二个填充字节
PORTB|=0B00000010;//clk = 1;
PORTB&=0B11111101; //clk = 0;
PORTB|=0B00000001;//cs = 1;
}
void delaynus(unsigned int n)
{
if (n == 0)
return ;
while (--n);
}
void main(void) //主函数
{
unsigned char i,j;
DDRA=0xff;
PORTA=0xff;
DDRB=0xff;
PORTB=0xff;
DDRD=0xff;
PORTD=0xff;
DDRC=0xff;
PORTC=0xff;
f=35000;
//打开第二级寄存器
PORTC&=0B11111011;//xfer=0;
PORTC&=0B11101111;//wr2=0;
PORTC|=0B00000010;//ILE=1;
while(1)
{
//DA_Conver(juchi[k]);
// DAC0832 CS=PC^0;
// ILE=PC^1;
// XFER=PC^2;
// WR1=PC^3;
// WR2=PC^4;
//打开第一级寄存器
PORTC&=0B11111110;//CS(PC0)=0;
//输入转换数据
PORTA=juchi[k];
PORTB=255-juchi[k];
delaynus(f);
PORTC&=0B11110111;//WR1=0;
delaynus(f);
PORTC|=0B00000001;//CS(PC0)=1;
PORTC|=0B00001000;//WR1=1;
//给出数据转换延时
//delaynus(f);
//delaynus(f);
//关闭第一级寄存器
//PORTC|=0B00000001;//CS(PC0)=1;
//PORTC|=0B00001000;//WR1=1;
//PORTC&=0B11111101;//ILE=0;
//打开第二级寄存器
//PORTC&=0B11111011;//xfer=0;
//PORTC&=0B11101111;//wr2=0;
//给出数据转换延时
//delaynus(f);
//delaynus(f);
//关闭第二级寄存器
//PORTC|=0B00000100;//xfer=1;
//PORTC&=0B00010000;//wr2=1;
//打开第一级寄存器
PORTC&=0B11111110;//CS(PC0)=0;
//输入转换数据
PORTA=juchi[k];
PORTB=255-juchi[k];
delaynus(f);
PORTC&=0B11110111;//WR1=0;
delaynus(f);
PORTC|=0B00000001;//CS(PC0)=1;
PORTC|=0B00001000;//WR1=1;
k++;
if(k==127) k=0;
delaynus(f);delaynus(f);delaynus(f);
delaynus(f);delaynus(f);delaynus(f);
delaynus(f);delaynus(f);delaynus(f);
}
}
|