完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
嗨,我试图实现简单的SPI通信打开8个LED连接到移位寄存器可以艾米帮助我错了这个代码,我得到随机输出,我想发送0x02打开LED(000000 10)。我的连接STCP(锁存器)移位寄存器到PoC0SHYCP(时钟)到PUTC3(SCK)对DATC5(SDO)的DS(数据选择)
以上来自于百度翻译 以下为原文 Hi I'm trying to implement simple spi communication to turn on 8 led connected to shift register can amy one help me whats wrong with this code, im getting random output, I want to send 0x02 to turn on led(00000010) . My connection ST_CP(LATCH) of shift register to PORTC0 SH_CP( CLOCK) to PORTC3(SCK) DS(Data select) to PORTC5(SDO) #include #include "spi.h" #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled) #pragma config LVP = OFF // Low-Voltage In-Circuit Serial Programming Enable bit #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit #pragma config WRT = OFF // Flash Program Memory Write Enable bits #pragma config CP = OFF // Flash Program Memory Code Protection bit #define _XTAL_FREQ 8000000 void main() { char data = 0x10; spiInit(SPI_MASTER_OSC_DIV4, SPI_DATA_SAMPLE_END, SPI_CLOCK_IDLE_LOW, SPI_IDLE_2_ACTIVE); TRISC0 = 0; RC0 = 1; RC3 = 0; RC5 = 0; while(1) { RC0 = 0; //Slave Select __delay_ms(100); spiWrite(data); while(!SSPSTATbits.BF); __delay_ms(100); RC0= 1; //Slave Deselect } } Attachment(s) spi_shiftreg.zip (2.90 KB) - downloaded 38 times |
|
相关推荐
19个回答
|
|
7HC595需要一个锁存时钟(RCK)把移位寄存器数据传送到输出锁存器,你是如何驱动那个信号的?
以上来自于百度翻译 以下为原文 The 74HC595 requires a latch clock (RCK) to transfer the shift register data to the output latch, how are you driving that signal? |
|
|
|
这和NOP一样。BF是一个只读位。
以上来自于百度翻译 以下为原文 SSPSTATbits.BF = 0; This is the same as a NOP. BF is a read only bit. |
|
|
|
如上所述,清除BF标志的唯一方法是读取SSPBUF寄存器。
以上来自于百度翻译 以下为原文 As above. The only way to clear the BF flag is to read the SSPBUF register. |
|
|
|
您可能不需要读取,但总是完成这样的交换。SSPBUF=DAT;而(!)SSPBUF;
以上来自于百度翻译 以下为原文 You may not need the read but always complete the exchange like this. SSPBUF = dat; while (!SSPSTATbits.BF); SSPBUF; |
|
|
|
使用SPI而不是闩销的优点是什么?我会使用中断来锁定在时间间隔上的7HC595。您可能需要外部设备的SPI。
以上来自于百度翻译 以下为原文 What is the advantage of using spi rather than latch pins? I would use interrupts to latch the 74HC595 at timed intervals. You may need spi for external devices. |
|
|
|
595个DS I连接到SDO(MOSI)和锁存引脚到RB5
以上来自于百度翻译 以下为原文 595 DS i connected to SDO(MOSI) And latch pin to RB5 |
|
|
|
您的代码将RB设置为0,并将其放在那里。每次将新的值写入到7HC595中时,应该将信号调低到高。
以上来自于百度翻译 以下为原文 Your code sets RB to 0, and leaves it there. It should be toggling that signal low to high each time you write a new value into the 74HC595. |
|
|
|
你能在没有SPI的情况下再次检查修改代码吗?我尝试逐个发送数据,当我以不工作的方式向SSPBUF发送数据时,它的工作是完美的。
以上来自于百度翻译 以下为原文 Can you please check modified code again without spi i tried sending data one by one its working perfectly when i send data to SSPBUF in a same way its not working Attachment(s) shift_reg_nospi.zip (2.31 KB) - downloaded 27 times |
|
|
|
您只附加了不使用SPI的代码,那么我们如何告诉您SPI代码有什么问题呢?
以上来自于百度翻译 以下为原文 You only attached the code that does not use SPI, so how can we tell you what is wrong with your SPI code? |
|
|
|
我在主线程上传了,这里是源码,请检查一下
以上来自于百度翻译 以下为原文 I uploaded in main thread anyway here is the source code please do check Attachment(s) spi_shiftreg.zip (2.90 KB) - downloaded 30 times |
|
|
|
当人们开始回答时,请不要改变最初的问题。这使讨论变得非常混乱。昨天我告诉你,清除BF标志的唯一方法是读取SSPBUF寄存器。你的新代码从不读SSPBUF寄存器,所以BF保持设置,所以等待循环不能正常工作。我不知道你是否写了SPI代码,但它是垃圾。每个人都会犯这样的错误:当它不是的时候,SPI就像对待USPART一样。SPI总是在主从之间同时传输。即使你不关心数据,你也必须为每一次传输做一次写和读。
以上来自于百度翻译 以下为原文 Please do NOT go changing the initial question once people start answering. That confuses the discussion terribly. I told you yesterday, the only way to clear the BF flag is to read the SSPBUF register. Your new code NEVER reads the SSPBUF register, so BF stays set, so the wait loops won't work correctly. I don't know if you wrote that SPI code, but it is rubbish. Everyone makes the mistake of trying to treat SPI like a USART when it is not. SPI is always a simultaneous TRANSFER between Master and Slave. You must do a write and read for every transfer, even if you don't care about the data. |
|
|
|
好吧,对不起,我对这个四人完全陌生,我保证我不会再犯这个错误了,因为你会回答我会尝试你的解决方案。
以上来自于百度翻译 以下为原文 Ok sorry I'm completely new to this fourm, I promise i'll not make this mistake again by confusing main thread anyway thank you for responding i'll try ur solution |
|
|
|
也许更清楚的是,这是一个同时交换数据的过程,在Saveto to Mead的同时,主人也会奴隶。他们仍然不会得到它,尽管……因为阅读+思维是困难的。眨眼:
以上来自于百度翻译 以下为原文 It might be more clear to emphasize that it's a simultaneous exchange of data, master-to-slave at the same time as slave-to-master. They still won't get it, though...because reading+thinking is hard. wink: |
|
|
|
同意,“Exchange”给出了比“转移”更清楚的指示。
以上来自于百度翻译 以下为原文 Agreed, "Exchange" gives a clearer indication than "Transfer". |
|
|
|
595个需要从PIC输出引脚3。数据、时钟和移位。我看到您已经使RC0=1。这将使它成为一个输入。如果你使用它作为595的锁存器,它应该是RC0= 0。在执行中把字节放入SSPBUF中,这将开始自动移出,然后对BF位进行加点,以确保8位都在缓冲器之外。然后,切换在你的情况下是RC0的换档线。当一个引脚是一个输出时,它被称为LATA、B、C等。如果在安装时查看引导日志,则有PORTA、B、C等(输入)、LATA、B、C等(输出),以及TrISA、B、C等(方向)。在程序上下文中,你必须使用适当的术语。
以上来自于百度翻译 以下为原文 The 595 needs 3 output pins from the PIC. Data, Clock, and Shift. I see you have made the RC0=1. That will make it an input. If you are using it as the 595's latch, it should be RC0=0. In execution place the byte into the SSPBUF, This will start the auto shift-out, then pole the BF bit to make sure the 8 bits are all out of the buffer. Then, toggle the Shift line which in your case is the RC0. FYI When a pin is an output it is called and referred to as lata,b,c,etc. Only if a pin is an do you call it a port. If one looks at the boot-log at setup there is porta,b,c,etc. (input) , lata,b,c,etc. (output) , and trisa,b,c,etc. (direction). In the program context you must use the proper terms. |
|
|
|
|
|
|
|
错了。你在考虑TrISC0位。该代码正确地使PIN为“TrISC0=0”的输出,该代码在传输后正在提升RC0。这部分是正确的。不对。您正在考虑PIC18F芯片和PIC16F1XXX芯片。PIC16F87没有任何LAT寄存器。没有一个“标准”的中型芯片。
以上来自于百度翻译 以下为原文 Wrong. You are thinking of the TRISC0 bit. The code is correctly making the pin an output with " TRISC0 = 0;" The code is raising RC0 after the transfer. That part is correct. Wrong. You are thinking of PIC18F chips and PIC16F1xxx chip. The PIC16F877 does not have any LAT registers. None of the "standard" midrange chips do. |
|
|
|
这表明MCC正在使用。MCC不支持PIC16F87A。
以上来自于百度翻译 以下为原文 That shows MCC being used. MCC does not support the PIC16F877A |
|
|
|
|
|
|
|
只有小组成员才能发言,加入小组>>
5161 浏览 9 评论
1999 浏览 8 评论
1928 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3171 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2226 浏览 5 评论
731浏览 1评论
613浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
503浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
629浏览 0评论
527浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 22:11 , Processed in 1.623873 second(s), Total 114, Slave 98 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号