ATmega16与TSL2561程序求助

2019-03-24 20:25发布

//ICC-AVR application builder : 2011-7-5 上午 09:21:59
// Target : M16
// Crystal: 4.0000Mhz #include <iom16v.h>
#include <macros.h>
#include <stdio.h> #define TSL2561_ADDRESS 0x29
#define TSL2561_WR     0x29
#define TSL2561_RD     0xA9 #define TWPS0 0
#define TWPS1 1
#define TWEN 2
#define TWIE 0
#define TWEA 6
#define TWINT 7
#define TWSTA 5
#define TWSTO 4
// Master
#define TW_START         0x08
#define TW_REP_START     0x10
// Master Transmitter
#define TW_MT_SLA_ACK    0x18
#define TW_MT_SLA_NACK   0x20
#define TW_MT_DATA_ACK   0x28
#define TW_MT_DATA_NACK  0x30
#define TW_MT_ARB_LOST   0x38
// Master Receiver
#define TW_MR_ARB_LOST   0x38
#define TW_MR_SLA_ACK    0x40
#define TW_MR_SLA_NACK   0x48
#define TW_MR_DATA_ACK   0x50
#define TW_MR_DATA_NACK  0x58
void twi_init(void)
{
// 2 Wire Bus initialization
// Generate Acknowledge Pulse: On
// 2 Wire Bus Slave Address: 0h
// General Call Recognition: Off
// Bit Rate: 250.000 kHz
  TWSR=0x00;
  TWBR=0x00;
  TWAR=0x00;
  TWCR=0x44;
  PORTC=0x03;
}
void PWM_init(void)
{
 DDRB=0xFF;
 PORTB=0XFF;
 TCCR0=0x61;
 OCR0=0;
}
unsigned char twi_start(void)//产生启动信号
{
 TWCR = (1 <<TWINT)|(1 <<TWSTA)|(1 <<TWEN);
 while(!(TWCR & (1 <<TWINT))){};
 return 1;
}
//产生停止信号
void twi_stop(void)
{
 TWCR = (1 <<TWINT)|(1 <<TWEN)|(1 <<TWSTO);
}
//向总线写一字节,并返回有无应答
unsigned char twi_write(unsigned char c)
{
  unsigned char ack=1;
  TWDR = c;
  TWCR = (1 <<TWINT)|(1 <<TWEN);
  while (!(TWCR & (1 <<TWINT))){};
  if((TWSR & 0xF8) != TW_MT_SLA_ACK)
    ack = 0;
  return ack;
}
//读一字节 ack: 1时应答,0时不应答
unsigned char twi_read(unsigned char ack)
{
  if (ack)
    TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA);  
  else
    TWCR = (1<<TWINT)|(1<<TWEN);
  while (!(TWCR & (1<<TWINT))){};  
  return(TWDR);
}
//延时程序
void delay_ms(unsigned int i)          
{
  while(i--)
  {                
  unsigned int j;          
    for(j=1;j<=1228;j++)  
    ;          
  }              
}
unsigned char TSL2561_read( unsigned char addr, unsigned char *c)
{
  unsigned char data;
 twi_start(); //开始
 twi_write(TSL2561_ADDRESS|TSL2561_WR);//写TSL2561地址
  twi_write(0x80|addr); //写命令
 twi_start(); //重新开始
 twi_write(TSL2561_ADDRESS|TSL2561_RD);//写TSL2561地址
 data=twi_read(c); //写数据
 twi_stop(); 
  delay_ms(10);
 return data;
}
void TSL2561_write( unsigned char addr, unsigned char c)
{
 twi_start(); //开始
 twi_write(TSL2561_ADDRESS|TSL2561_WR);//写TSL2561地址
  twi_write(0x80|addr); //写命令
 twi_write(c); //写数据
 twi_stop(); //停止
  delay_ms(10); //延时10 ms
}
unsigned float calculux(unsigned float a,unsigned float b)//计算光强
{
unsigned float c,d;
d=a/b;
if(d>0&&d<=0.5)
  c=0.0304*a-0.062*a*sqrt(sqrt(d));
else if(d<=0.61)
  c=0.0224*a-0.031*b;
else if(d<=0.8)
  c=0.0128*a-0.0153*b;
else if(d<=1.3)
  c=0.00146*a-0.00112*b;
else if(d=0||d>1.3)
  c=0;
return(c);
}
void PWM(unsigned char m)//PWM信号
{
if(m<1) OCR0=255;//D=100
else if(m<=10) OCR0=229;//D=90
else if(m<=50) OCR0=204;//D=80
else if(m<=80) OCR0=140;//D=55
else if(m<=155) OCR0=76;//D=30
else if(m<=230) OCR0=38;//D=15
else OCR0=0;//D=0
}
int main(void)
{
unsigned int CH0_LOW,CH0_HIGH,CH1_LOW,CH1_HIGH,CH0,CH1,LX;
twi_init();
PWM_init();
TSL2561_write(TSL2561_ADDRESS,0X00,0X03);//设置tsl2561的控制器,tsl2561上电
TSL2561_write(TSL2561_ADDRESS,0X01,0X02);//设置积分时间402ns
TSL2561_write(TSL2561_ADDRESS,0X02,0X00);
TSL2561_write(TSL2561_ADDRESS,0X03,0X00);
TSL2561_write(TSL2561_ADDRESS,0X06,0X00);//关闭中断
while(1)
{
delay_ms(200);
CH0_LOW=TSL2561_READ(TSL2561_ADDRESS,0x0c);//通道0的低八位
CH0_HIGH=TSL2561_READ(TSL2561_ADDRESS,0x0d);//通道0的高八位
CH1_LOW=TSL2561_READ(TSL2561_ADDRESS,0X0e);//通道1的低8位
CH1_HIGH=TSL2561_READ(TSL2561_ADDRESS,0X0f);//通道1的高8位
CH0=256*CH0_HIGH+CH0_LOW;
CH1=256*CH1_HIGH+CH1_LOW;
LX=calculux(CH0,CH1);//计算强度模块
PWM(LX);//产生脉冲信号
};
} 利用ICC AVR编译后出现如下提示: !E E:ICCprojectTSL2561.c(101): illegal character `241'
!E E:ICCprojectTSL2561.c(101): illegal character `241'
!E E:ICCprojectTSL2561.c(102): illegal character `241'
!E E:ICCprojectTSL2561.c(102): illegal character `241'
!E E:ICCprojectTSL2561.c(104): illegal character `241'
!E E:ICCprojectTSL2561.c(104): illegal character `241'
!E E:ICCprojectTSL2561.c(105): illegal character `241'
!E E:ICCprojectTSL2561.c(105): illegal character `241'
!E E:ICCprojectTSL2561.c(106): illegal character `241'
!E E:ICCprojectTSL2561.c(106): illegal character `241'
!E E:ICCprojectTSL2561.c(106): type error in argument 1 to `twi_read'; found `pointer to unsigned char' expected `unsigned char'
!E E:ICCprojectTSL2561.c(107): illegal character `241'
!E E:ICCprojectTSL2561.c(107): illegal character `241'
!E E:ICCprojectTSL2561.c(107): illegal character `241'
!E E:ICCprojectTSL2561.c(107): illegal character `241'
!E E:ICCprojectTSL2561.c(109): illegal character `241'
!E E:ICCprojectTSL2561.c(109): illegal character `241'
!E E:ICCprojectTSL2561.c(113): illegal character `241'
!E E:ICCprojectTSL2561.c(113): illegal character `241'
!E E:ICCprojectTSL2561.c(114): illegal character `241'
!E E:ICCprojectTSL2561.c(114): too many errors 即如程序中蓝 {MOD}部分所示,请高人指点下
此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
3条回答
hm3lin
2019-03-25 17:52
unsigned float calculux(unsigned float a,unsigned float b)//计算光强
{
unsigned float c,d;
d=a/b;
if(d>0&&d<=0.5)
  c=0.0304*a-0.062*a*sqrt(sqrt(d));
最后这句,datasheet上应该是0.0304*a-0.062*a*(d的1.4次方)吧?

一周热门 更多>