nrf51822入门
前言
对于嵌入式行业的我而言这
一、延时函数
#include "nrf_delay.h"
nrf_delay_us(4);
二、使用步骤
1.引入库
代码如下(示例):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
2.iic
myiic.c
#include "myiic.h"
#include <stdint.h>
#include <string.h>
#include "nordic_common.h"
#include "nrf.h"
#include "nrf51_bitfields.h"
#include "ble_hci.h"
#include "ble_advdata.h"
#include "ble_conn_params.h"
#include "softdevice_handler.h"
#include "app_timer.h"
#include "app_button.h"
#include "ble_nus.h"
#include "simple_uart.h"
#include "boards.h"
#include "ble_error_log.h"
#include "ble_debug_assert_handler.h"
#include "nrf_delay.h"
//³õʼ»¯IIC
void IIC_Init(void)
{
nrf_gpio_cfg_output(IIC_SDA_PIN);
nrf_gpio_cfg_output(IIC_SCL_PIN);
IIC_SDA_H;
IIC_SCL_H;
//GPIO_SetBits(GPIOB,GPIO_Pin_6|GPIO_Pin_7); //PB6,PB7 Êä³ö¸ß
}
//²úÉúIICÆðʼÐźÅ
void IIC_Start(void)
{
SDA_OUT(); //sdaÏßÊä³ö
IIC_SDA_H;
IIC_SCL_H;
nrf_delay_us(4);
IIC_SDA_L;//START:when CLK is high,DATA change form high to low
nrf_delay_us(4);
IIC_SCL_L;//ǯסI2C×ÜÏߣ¬×¼±¸·¢ËÍ»ò½ÓÊÕÊý¾Ý
}
//²úÉúIICÍ£Ö¹ÐźÅ
void IIC_Stop(void)
{
SDA_OUT();//sdaÏßÊä³ö
IIC_SCL_L;
IIC_SDA_L;//STOP:when CLK is high DATA change form low to high
nrf_delay_us(4);
IIC_SCL_H;
nrf_delay_us(4);
IIC_SDA_H;//·¢ËÍI2C×ÜÏß½áÊøÐźÅ
}
//µÈ´ýÓ¦´ðÐźŵ½À´
//·µ»ØÖµ£º1£¬½ÓÊÕÓ¦´ðʧ°Ü
// 0£¬½ÓÊÕÓ¦´ð³É¹¦
u8 IIC_Wait_Ack(void)
{
u8 ucErrTime=0;
SDA_IN(); //SDAÉèÖÃΪÊäÈë
IIC_SDA_H;nrf_delay_us(2);
IIC_SCL_H;nrf_delay_us(2);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
IIC_Stop();
return 1;
}
}
IIC_SCL_L;//ʱÖÓÊä³ö0
return 0;
}
//²úÉúACKÓ¦´ð
void IIC_Ack(void)
{
IIC_SCL_L;
SDA_OUT();
IIC_SDA_L;
nrf_delay_us(2);
IIC_SCL_H;
nrf_delay_us(2);
IIC_SCL_L;
}
//²»²úÉúACKÓ¦´ð
void IIC_NAck(void)
{
IIC_SCL_L;
SDA_OUT();
IIC_SDA_H;
nrf_delay_us(2);
IIC_SCL_H;
nrf_delay_us(2);
IIC_SCL_L;
}
//IIC·¢ËÍÒ»¸ö×Ö½Ú
//·µ»Ø´Ó»úÓÐÎÞÓ¦´ð
//1£¬ÓÐÓ¦´ð
//0£¬ÎÞÓ¦´ð
void IIC_Send_Byte(u8 txd)
{
u8 t;
SDA_OUT();
IIC_SCL_L;//ÀµÍʱÖÓ¿ªÊ¼Êý¾Ý´«Êä
for(t=0;t<8;t++)
{
//IIC_SDA=(txd&0x80)>>7;
if((txd&0x80)>>7)
IIC_SDA_H;
else
IIC_SDA_L;
txd<<=1;
nrf_delay_us(1); //¶ÔTEA5767ÕâÈý¸öÑÓʱ¶¼ÊDZØÐëµÄ
IIC_SCL_H;
nrf_delay_us(2);
IIC_SCL_L;
nrf_delay_us(2);
}
}
//¶Á1¸ö×Ö½Ú£¬ack=1ʱ£¬·¢ËÍACK£¬ack=0£¬·¢ËÍnACK
u8 IIC_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
// SDA_OUT();
// IIC_SDA=1;nrf_delay_us(2); //δ¼ÓÉÏÀµç×èʱ ÊÖ¶¯À¸ßÊý¾ÝÏß
SDA_IN();//SDAÉèÖÃΪÊäÈë
for(i=0;i<8;i++ )
{
IIC_SCL_L;
nrf_delay_us(2);
IIC_SCL_H;
receive<<=1;
if(READ_SDA)receive++;
nrf_delay_us(2);
}
if (!ack)
IIC_NAck();//·¢ËÍnACK
else
IIC_Ack(); //·¢ËÍACK
return receive;
}
myiic.h
#ifndef __MYIIC_H
#define __MYIIC_H
typedef unsigned char u8;
typedef unsigned short int u16;
typedef unsigned int u32;
//IO·½ÏòÉèÖÃ
#define SDA_IN() nrf_gpio_cfg_sense_input(IIC_SDA_PIN, BUTTON_PULL, NRF_GPIO_PIN_SENSE_LOW);
#define SDA_OUT() nrf_gpio_cfg_output(IIC_SDA_PIN)
//IO²Ù×÷º¯Êý
#define IIC_SCL_L nrf_gpio_pin_clear(IIC_SCL_PIN)
#define IIC_SCL_H nrf_gpio_pin_set(IIC_SCL_PIN)
//IO²Ù×÷º¯Êý
#define IIC_SDA_L nrf_gpio_pin_clear(IIC_SDA_PIN)
#define IIC_SDA_H nrf_gpio_pin_set(IIC_SDA_PIN)
#define READ_SDA nrf_gpio_pin_read(IIC_SDA_PIN)
//IICËùÓвÙ×÷º¯Êý
void IIC_Init(void); //³õʼ»¯IICµÄIO¿Ú
void IIC_Start(void); //·¢ËÍIIC¿ªÊ¼ÐźÅ
void IIC_Stop(void); //·¢ËÍIICÍ£Ö¹ÐźÅ
void IIC_Send_Byte(u8 txd); //IIC·¢ËÍÒ»¸ö×Ö½Ú
u8 IIC_Read_Byte(unsigned char ack);//IIC¶ÁÈ¡Ò»¸ö×Ö½Ú
u8 IIC_Wait_Ack(void); //IICµÈ´ýACKÐźÅ
void IIC_Ack(void); //IIC·¢ËÍACKÐźÅ
void IIC_NAck(void); //IIC²»·¢ËÍACKÐźÅ
void IIC_Write_One_Byte(u8 daddr,u8 addr,u8 data);
u8 IIC_Read_One_Byte(u8 daddr,u8 addr);
#endif
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。