📄 upsspymast_ad.c
字号:
/*----------------------------------------------------------------
*File Name: UPSSpyMain_AD.c -
*Description: 从TLC1543读取采样值,形参port是采样的通道号 -
*Project: -
*MCU type: Mega162 -
-
*Company: WY -
*Compiler: ICCAVR6.30 -
*MODIFY: 郭准 06.3.19 -
-----------------------------------------------------------------*/
#include "global.h"
//-----------------TLC1543 端口定义
#define CLOCK 0 //PORTC的0位
#define D_IN 1 //PORTC的1位
#define D_OUT 2 //PORTC的2位
#define _CS 3 //PORTC的3位
//-----------------端口电平变化的 定义
#define CLOCK_0 PORTC&=~(1<<CLOCK)
#define CLOCK_1 PORTC|=(1<<CLOCK)
#define D_IN_0 PORTC&=~(1<<D_IN)
#define D_IN_1 PORTC|=(1<<D_IN)
#define D_OUT_0 PORTC&=~(1<<D_OUT)
#define D_OUT_1 PORTC|=(1<<D_OUT)
#define _CS_0 PORTC&=~(1<<_CS)
#define _CS_1 PORTC|=(1<<_CS)
extern void send0_char_com(uchar c);
extern void send0_string_com(uchar *str,uchar len);
uchar ad_buf[5] = {0,0,0,0,0};
//------------------------------------------------------
//功能:从TLC1543读取采样值
//入口:channel = 通道号 .0:0通道;1:1通道。。。。。。。
//出口:
//设计:郭准,伟业,2006/3/19
//------------------------------------------------------
uint read1543(uchar channel)
{
uint ad;
uint i;
uchar al=0;
uchar ah=0;
CLOCK_0; //时钟源初始为低
_CS_0; //片选为低,选中芯片
//---------------等待一个设置时间+两个内部系统时钟的下降沿
for(i=0;i<21;i++)
{;}
//---------------把通道号打入1543
channel<<=4; //左移4位的作用是为了,下面高位比较方便
al=0;ah=0;
for (i=0;i<4;i++)
{
if((channel&0x80))
{
D_IN_1;
nop();
}
else
{
D_IN_0;
nop();
}
CLOCK_1; //时钟的下降沿 锁存数据,地址移入
nop();
CLOCK_0;
channel<<=1;
}
//---------------//填充6个CLOCK
for (i=0;i<6;i++) //采样一直持续6个 I/O CLOCK周期
{
CLOCK_1;
nop();
CLOCK_0;
nop();
}
nop();
//---------------//等待AD转换
_CS_1;
for(i=0;i<12;i++) //多个单周期延时nop,微秒级延时
{
nop();
}
_CS_0;
nop();
//---------------//取D9,D8
for (i=0;i<2;i++)
{
// D_OUT_1;
ah<<=1;
CLOCK_1;
if (PINC&(1<<D_OUT))
{
ah|=0x01;
}
CLOCK_0;
}
//---------------//取D7--D0
for (i=0;i<8;i++)
{
al<<=1;
CLOCK_1;
if (PINC&(1<<D_OUT))// if (PINC&(1<<D_OUT))
{
al|=0x01;
}
CLOCK_0;
}
_CS_1; // 关闭1543
//---------------//得到AD值
ad=(uint)ah;
ad<<=8;
ad|=al;
return (ad);
}
void ad_change(uchar channel)
{
uint temp;
temp = read1543(channel);
ad_buf[0] = temp/1000+0x30;
ad_buf[1] = (temp%1000)/100+0x30;
ad_buf[2] = (temp%100)/10+0x30;
ad_buf[3] = temp%10+0x30;
send0_string_com(ad_buf,4);
// send0_char_com(0x0d);
// send0_char_com(0x0a);
}
void more_ad_change(void)
{
send0_string_com("0-5 channel AD = ",17);
ad_change(0);
send0_char_com(' ');
ad_change(1);
send0_char_com(' ');
ad_change(2);
send0_char_com(' ');
ad_change(3);
send0_char_com(' ');
ad_change(4);
send0_char_com(' ');
ad_change(5);
send0_char_com(' ');
send0_char_com(0x0d);
send0_char_com(0x0a);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -