首页 [指南]iccavr下读写ov6620芯片外部寄存器的代码

[指南]iccavr下读写ov6620芯片外部寄存器的代码

举报
开通vip

[指南]iccavr下读写ov6620芯片外部寄存器的代码[指南]iccavr下读写ov6620芯片外部寄存器的代码 ICCAVR下读写OV6620芯片内部寄存器的代码 ,,,,,,下读写,,,,,,芯片内部寄存器的代码,使用的是,,,,,,,,,, /* TWSR values (not bits) */ /* Master */ #define TW_START 0x08 #define TW_REP_START 0x10 /* Master Transmitter */ #define TW_MT_SLA_ACK 0x18 #define TW_MT_S...

[指南]iccavr下读写ov6620芯片外部寄存器的代码
[指南]iccavr下读写ov6620芯片外部寄存器的代码 ICCAVR下读写OV6620芯片内部寄存器的代码 ,,,,,,下读写,,,,,,芯片内部寄存器的代码,使用的是,,,,,,,,,, /* TWSR values (not bits) */ /* 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 /* Slave Transmitter */ #define TW_ST_SLA_ACK 0xA8 #define TW_ST_ARB_LOST_SLA_ACK 0xB0 #define TW_ST_DATA_ACK 0xB8 #define TW_ST_DATA_NACK 0xC0 #define TW_ST_LAST_DATA 0xC8 /* Slave Receiver */ #define TW_SR_SLA_ACK 0x60 #define TW_SR_ARB_LOST_SLA_ACK 0x68 #define TW_SR_GCALL_ACK 0x70 #define TW_SR_ARB_LOST_GCALL_ACK 0x78 #define TW_SR_DATA_ACK 0x80 #define TW_SR_DATA_NACK 0x88 #define TW_SR_GCALL_DATA_ACK 0x90 #define TW_SR_GCALL_DATA_NACK 0x98 #define TW_SR_STOP 0xA0 /* Misc */ #define TW_NO_INFO 0xF8 #define TW_BUS_ERROR 0x00 /* * The lower 3 bits of TWSR are reserved on the ATmega163. * The 2 LSB carry the prescaler bits on the newer ATmegas. */ #define TW_STATUS_MASK (_BV(TWS7)|_BV(TWS6)|_BV(TWS5)|_BV(TWS4)|_BV(TWS3)) #define TW_STATUS (TWSR & TW_STATUS_MASK) /* * R/~W bit in SLA+R/W address field. */ #define TW_READ 1 #define TW_WRITE 0 /* 2-wire Status Register - TWSR */ #define TWS7 7 #define TWS6 6 #define TWS5 5 #define TWS4 4 #define TWS3 3 #define TWPS1 1 #define TWPS0 0 #define _BV(bit) (1 << (bit)) #define TWI_SLA_CAM 0xc0 //Cam C0 sensor 90 #define MAX_ITER 200 #define PAGE_SIZE 8 // before 8 int32 i2c_read_bytes(uint16 eeaddr, int32 len, uint8 *buf) { uint32 counter=0; uint8 sla, twcr, n = 0; uint32 rv = 0; /* patch high bits of EEPROM address into SLA */ sla = TWI_SLA_CAM |(((eeaddr >> 8) & 0x07) << 1); //sla=0XC1; /* * Note [6] * First cycle: master transmitter mode */ //进入start状态 restart: if (n++ >= MAX_ITER) return -1; begin: TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); /* send start condition */ while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */ switch ((TW_STATUS)) { case TW_REP_START: /* OK, but should not happen */ case TW_START: break; case TW_MT_ARB_LOST: /* Note [7] */ goto begin; default: return -1; /* error: not in start condition */ /* NB: do /not/ send stop condition */ } //先写芯片从地址0XC0 /* Note [8] */ /* send SLA+W */ TWDR = sla | TW_WRITE; TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */ while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */ switch ((TW_STATUS)) { case TW_MT_SLA_ACK: break; case TW_MT_SLA_NACK: /* nack during select: device busy writing */ /* Note [9] */ goto restart; case TW_MT_ARB_LOST: /* re-arbitrate */ goto begin; default: goto error; /* must send stop condition */ } //再写入寄存器的地址 TWDR = eeaddr; /* low 8 bits of addr */ TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */ while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */ switch ((TW_STATUS)) { case TW_MT_DATA_ACK: break; case TW_MT_DATA_NACK: goto quit; case TW_MT_ARB_LOST: goto begin; default: goto error; /* must send stop condition */ } //停止TWI接口,并等待ov6620来识别写入的数据 TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN); /* send stop condition */ //TWCR = 0; // Stop the twi interface to make the camera able to rescognise the new start while (counter != 0x0040) { counter++; } /* * Note [10] * Next cycle(s): master receiver mode */ //再次进入start状态 TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); /* send (rep.) start condition */ while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */ switch ((TW_STATUS)) { case TW_START: /* OK, but should not happen */ case TW_REP_START: break; case TW_MT_ARB_LOST: goto begin; default: goto error; } //发送芯片读地址 /* send SLA+R */ TWDR = (sla | 0x01); TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */ while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */ switch ((TW_STATUS)) { case TW_MR_SLA_ACK: break; case TW_MR_SLA_NACK: goto quit; case TW_MR_ARB_LOST: goto begin; default: goto error; } //根据读取字节的长度,做了一个循环 for (twcr = _BV(TWINT) | _BV(TWEN) | _BV(TWEA) /* Note [11] */; len > 0; len--) { if (len == 1) twcr = _BV(TWINT) | _BV(TWEN); /* send NAK this time */ TWCR = twcr; /* clear int to start transmission */ while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */ switch ((TW_STATUS)) { case TW_MR_DATA_NACK: len = 0; /* force end of loop */ /* FALLTHROUGH */ case TW_MR_DATA_ACK: *buf++ = TWDR; rv++; break; default: goto error; } } quit: /* Note [12] */ TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN); /* send stop condition */ return rv; error: rv = -1; goto quit; } int32 i2c_write_page(uint16 eeaddr, int32 len, uint8 *buf) { uint8 sla, n = 0; int32 rv = 0; uint16 endaddr; if (eeaddr + len < (eeaddr | (PAGE_SIZE - 1))) endaddr = eeaddr + len; else endaddr = (eeaddr | (PAGE_SIZE - 1)) + 1; len = endaddr - eeaddr; /* patch high bits of EEPROM address into SLA */ sla = TWI_SLA_CAM | (((eeaddr >> 8) & 0x07) << 1); //sla=0xc0; restart: if (n++ >= MAX_ITER) return -1; begin: //发送start状态 /* Note 13 */ TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); /* send start condition */ while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */ switch ((TW_STATUS)) { case TW_REP_START: /* OK, but should not happen */ case TW_START: break; case TW_MT_ARB_LOST: goto begin; default: return -1; /* error: not in start condition */ /* NB: do /not/ send stop condition */ } //发送芯片写地址 /* send SLA+W */ TWDR = sla | TW_WRITE; TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */ while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */ switch ((TW_STATUS)) { case TW_MT_SLA_ACK: break; case TW_MT_SLA_NACK: /* nack during select: device busy writing */ goto restart; case TW_MT_ARB_LOST: /* re-arbitrate */ goto begin; default: goto error; /* must send stop condition */ } TWDR = eeaddr; /* low 8 bits of addr */ TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */ while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */ switch ((TW_STATUS)) { case TW_MT_DATA_ACK: break; case TW_MT_DATA_NACK: goto quit; case TW_MT_ARB_LOST: goto begin; default: goto error; /* must send stop condition */ } for (; len > 0; len--) { TWDR = *buf++; TWCR = _BV(TWINT) | _BV(TWEN); /* start transmission */ while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */ switch ((TW_STATUS)) { case TW_MT_DATA_NACK: goto error; /* device write protected -- Note [14] */ case TW_MT_DATA_ACK: rv++; break; default: goto error; } } quit: TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN); /* send stop condition */ return rv; error: rv = -1; goto quit; } int32 i2c_write_bytes(uint16 eeaddr, int32 len, uint8 *buf) { int32 rv, total; total = 0; do { rv = i2c_write_page(eeaddr, len, buf); if (rv == -1) return -1; eeaddr += rv; len -= rv; buf += rv; total += rv; } while (len > 0); return total; } int32 write_register(uint16 numregister, uint8 value){ uint8 *pvalue; int32 num; pvalue = &value; num = i2c_write_bytes(numregister, 1, pvalue); if(num!=1) return -1; else return 1; } int32 read_register(uint16 numregister){ int32 num; uint8 *pvalue; uint8 value = 0; pvalue = &value; num = i2c_read_bytes(numregister, 1, pvalue); if(num!=1) return -1; else return (int)*pvalue; }
本文档为【[指南]iccavr下读写ov6620芯片外部寄存器的代码】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_633423
暂无简介~
格式:doc
大小:34KB
软件:Word
页数:0
分类:生活休闲
上传时间:2017-10-16
浏览量:12