新手入门
- 积分
- 26
- 金钱
- 26
- 注册时间
- 2015-8-12
- 在线时间
- 0 小时
|
5金钱
最近玩stm8,想把24L01的程序移植到stm8. 把引脚定义好,程序基本没有变化,程序可以在51运行,但是在stm8上却不行, 后来直接写寄存器后在读,还是不成功,这里请问大家 51向stm8 移植 应注意什么问题,下面附上程序 看看是哪里的问题,谢谢大家<br />
<br />
<br />
#include "IOSTM8S103F3.h"<br />
<br />
/************************I/O口定义********************************/<br />
#define LED PB_ODR_ODR5<br />
<br />
#define CSN PB_ODR_ODR4<br />
<br />
#define SCK PC_ODR_ODR3<br />
#define MOSI PC_ODR_ODR4<br />
#define MISO PC_IDR_IDR5<br />
//#define IRQ PC_IDR_IDR6<br />
#define CE PC_ODR_ODR7<br />
<br />
void Delay(unsigned int x)<br />
{<br />
unsigned int i,j;<br />
for(i=x; i>0; i--)<br />
{<br />
for(j=110; j>0; j--) <br />
{<br />
++j;<br />
--j;<br />
}<br />
}<br />
}<br />
<br />
void InerDelay_us(unsigned char n)<br />
{<br />
for(;n>0;n--)<br />
{<br />
++n;<br />
--n;<br />
}<br />
}<br />
<br />
unsigned char SPI_RW(unsigned char uuchar)<br />
{<br />
unsigned char bit_ctr;<br />
for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit<br />
{<br />
MOSI = (uuchar & 0x80)>>7; // output 'uchar', MSB to MOSI<br />
uuchar = (uuchar << 1); // shift next bit into MSB..<br />
SCK = 1; // Set SCK high..<br />
//Delay(1); /*****************/<br />
uuchar |= MISO; // capture current MISO bit<br />
SCK = 0; // ..then set SCK low again<br />
//Delay(1); /*************************/<br />
}<br />
return(uuchar); // return read uchar<br />
}<br />
<br />
unsigned char SPI_Read(unsigned char reg)<br />
{<br />
unsigned char reg_val;<br />
<br />
CSN = 0; // CSN low, initialize SPI communication...<br />
SPI_RW(reg); // Select register to read from..<br />
reg_val = SPI_RW(0); // ..then read registervalue<br />
CSN = 1; // CSN high, terminate SPI communication<br />
<br />
return(reg_val); // return register value<br />
}<br />
<br />
<br />
<br />
<br />
<br />
unsigned char SPI_RW_Reg(unsigned char reg, unsigned char value)<br />
{<br />
unsigned char status;<br />
<br />
CSN = 0; // CSN low, init SPI transaction<br />
InerDelay_us(10);<br />
status = SPI_RW(reg); // select register<br />
SPI_RW(value); // ..and write value to it..<br />
CSN = 1; // CSN high again<br />
<br />
return(status); // return nRF24L01 status uchar<br />
}<br />
<br />
<br />
void Init_GPIO(void)<br />
{<br />
//#define LED PB_ODR_ODR5<br />
<br />
//#define CSN PB_ODR_ODR4<br />
//#define SCK PC_ODR_ODR3<br />
//#define MOSI PC_ODR_ODR4<br />
//#define MISO PC_IDR_IDR5<br />
//#define IRQ PC_IDR_IDR6<br />
//#define CE PC_ODR_ODR7<br />
<br />
PB_DDR_DDR5 = 1;<br />
PB_DDR_DDR4 = 1;<br />
PB_CR1 = 0xff; <br />
PB_CR2 = 0x00;<br />
<br />
PC_DDR_DDR3 = 1;<br />
PC_DDR_DDR4 = 1;<br />
PC_DDR_DDR5 = 0;<br />
PC_DDR_DDR6 = 0;<br />
PC_DDR_DDR7 = 1;<br />
PC_CR1 = 0xff; <br />
PC_CR2 = 0x00;<br />
} <br />
<br />
void Init_Cloc(void)<br />
{<br />
CLK_ICKR|=0x01; //开启内部HSI<br />
while(!(CLK_ICKR&0x02));//HSI准备就绪<br />
CLK_SWR=0xe1; //HSI为主时钟源<br />
CLK_CKDIVR=0x18; //HSI 8 分频 2M <br />
}<br />
<br />
int main(void)<br />
{<br />
Init_Cloc();<br />
<br />
Init_GPIO();<br />
LED = 1;<br />
CE=0; <br />
CSN=1; <br />
SCK=0;<br />
<br />
SPI_RW_Reg(0x20 + 0x06, 0x07); //0x20是读寄存器 0x06寄存器地址 <br />
while(1)<br />
{<br />
<br />
if(SPI_Read(0x06) == 0x07) //查看是否发送成功<br />
{<br />
LED = 0; <br />
Delay(600);<br />
LED = 1; <br />
Delay(600); <br />
}<br />
<br />
}<br />
<br />
}<br /> |
最佳答案
查看完整内容[请看2#楼]
回复【2楼】正点原子:
---------------------------------
谢谢站长! 我的问题找到了,弄了好长时间才找到,是杜邦线的问题,杜邦线这东西不靠谱,程序没有问题,不过我把分频都调到了最大值。
|