完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
你好,我正在开发一个PIC16F150 3微控制器的项目,我需要使用I2C接口主模式。不幸的是,它似乎不起作用:我在SDA和SCK上没有看到任何数据或时钟。我已经创建了一个小应用程序,在I2C总线上连续发送数据,以便调试这个问题:我配置的RC4只查看程序是否在我的示波器上运行。附加信息:工具链:XC8 V1.42程序员:PICtiT2(没有调试可用的这个设备)我的代码有什么问题?最好的问候。
以上来自于百度翻译 以下为原文 Hello, I'm developing a project with PIC16F1503 microcontroller and I need to use the I2C interface Master mode. Unfortunately it seems not working: I don't see any data or clock on SDA and SCK. I have created a small application, continuously sending data on I2C bus, in order to debug this problem: /* * File: main.c * Author: XXX * * Created on 15 marzo 2018, 7.45 */ // CONFIG1 #pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin) #pragma config WDTE = ON // Watchdog Timer Enable (WDT enabled) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LPBOR = OFF // Low-Power Brown Out Reset (Low-Power BOR is disabled) #pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled) #include #define _XTAL_FREQ 16000000 void I2C_Master_Init(const unsigned long c) { SSP1CON1 = 0b00101000; //SSP Module as Master SSP1CON2 = 0; SSP1ADD = (_XTAL_FREQ/(4*c))-1; //Setting Clock Speed SSP1STAT = 0; TRISC0 = 1; //Setting as input as given in datasheet TRISC1 = 1; //Setting as input as given in datasheet } void I2C_Master_Wait() { while ((SSP1STAT & 0x01) || (SSP1CON2 & 0x1F)); //Transmit is in progress } void I2C_Master_Start() { I2C_Master_Wait(); SEN = 1; //Initiate start condition } void I2C_Master_RepeatedStart() { I2C_Master_Wait(); RSEN = 1; //Initiate repeated start condition } void I2C_Master_Stop() { I2C_Master_Wait(); PEN = 1; } void I2C_Master_Write(unsigned d) { I2C_Master_Wait(); SSP1BUF = d; //Write data to SSPBUF } unsigned short I2C_Master_Read(unsigned short a) { unsigned short temp; I2C_Master_Wait(); RCEN = 1; I2C_Master_Wait(); temp = SSP1BUF; //Read data from SSPBUF I2C_Master_Wait(); ACKDT = (a)?0:1; //Acknowledge bit ACKEN = 1; //Acknowledge sequence return temp; } void main(void) { OSCCON = 0x7A; TRISC4 = 0; I2C_Master_Init(100000); while (1) { RC4 = 1; I2C_Master_Start(); //Start condition I2C_Master_Write(0x30); //7 bit address + Write I2C_Master_Write(0x57); //Write data I2C_Master_Stop(); //Stop condition RC4 = 0; } return; } I configured RC4 only to see if the program is running or not with my oscilloscope. Additional information: Toolchain: XC8 v1.42 Programmer: PICkit2 (no debug available for this device) Any idea on what's wrong with my code? Best Regards. |
|
相关推荐
2个回答
|
|
PORTC在复位时默认为模拟模式,必须清除ANSELC SFR中的位,以将RC0和RC1设置成数字模式。
以上来自于百度翻译 以下为原文 PORTC defaults to analog mode on reset, you must clear the bits in the ANSELC SFR to set RC0 and RC1 into digital mode. |
|
|
|
您好杰克@ KKStack,谢谢您的帮助。现在我看到I2C总线时钟数据,但我仍然遇到一些问题。你可以从我的代码中看到:我在I2C:0x30(7bit地址+W位)和0x56上写了2个字节,但是似乎第二个字节根本没有被录入(见附图)。最好的问候。
以上来自于百度翻译 以下为原文 Hello jack@kksound, thanks for your help. Now I see I2C bus clocking out data, but I'm still experiencing some problems. As you can see from my code: /* * File: main.c * Author: XXX * * Created on 15 marzo 2018, 7.45 */ // CONFIG1 #pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin) #pragma config WDTE = ON // Watchdog Timer Enable (WDT enabled) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LPBOR = OFF // Low-Power Brown Out Reset (Low-Power BOR is disabled) #pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled) #include #define _XTAL_FREQ 16000000 void I2C_Master_Init(const unsigned long c) { SSP1CON1 = 0b00101000; //SSP Module as Master SSP1CON2 = 0; SSP1ADD = (_XTAL_FREQ/(4*c))-1; //Setting Clock Speed SSP1STAT = 0; TRISC0 = 1; //Setting as input as given in datasheet TRISC1 = 1; //Setting as input as given in datasheet } void I2C_Master_Wait() { while ( (SEN == 1) || (RSEN == 1) || (PEN == 1) || (RCEN == 1) || (SSP1STAT&0x04) ); //while ((SSP1STAT & 0x01) || (SSP1CON2 & 0x1F)); //Transmit is in progress } void I2C_Master_Start() { //I2C_Master_Wait(); SEN = 1; //Initiate start condition while (SEN == 1); } void I2C_Master_RepeatedStart() { //I2C_Master_Wait(); RSEN = 1; //Initiate repeated start condition while (RSEN == 1); } void I2C_Master_Stop() { //I2C_Master_Wait(); PEN = 1; while (PEN == 1); } void I2C_Master_Write(unsigned d) { //I2C_Master_Wait(); SSP1BUF = d; //Write data to SSPBUF while (BF == 1); } unsigned short I2C_Master_Read(unsigned short a) { unsigned short temp; //I2C_Master_Wait(); RCEN = 1; while(BF==0); //I2C_Master_Wait(); temp = SSP1BUF; //Read data from SSPBUF I2C_Master_Wait(); ACKDT = (a)?0:1; //Acknowledge bit ACKEN = 1; //Acknowledge sequence return temp; } void main(void) { OSCCON = 0x7A; TRISC4 = 0; ANSC0 = 0; ANSC1 = 0; NCO1SEL = 1; // NCO1 on RA4 I2C_Master_Init(100000); while (1) { RC4 = 1; I2C_Master_Start(); //Start condition I2C_Master_Write(0x30); //7 bit address + Write I2C_Master_Write(0x57); //Write data I2C_Master_Stop(); //Stop condition RC4 = 0; for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++) { } } } return; } I'm writing 2 bytes on I2C: 0x30 (7bit address + W bit) and 0x56, but it seems that the second byte is not clocked out at all (see attached images). Maybe anything else wrong with I2C configuration? Best Regards. Attached Image(s) |
|
|
|
只有小组成员才能发言,加入小组>>
5160 浏览 9 评论
1998 浏览 8 评论
1927 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3170 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2225 浏览 5 评论
727浏览 1评论
612浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
501浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
626浏览 0评论
524浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 01:56 , Processed in 1.244540 second(s), Total 79, Slave 63 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号