编程 arm 单片机 c语言 cotex m3 嵌入式 问题如下。 一定采纳 求高手回答

在对cortex m0的外设I2c进行初始化时候,需要设置很多寄存器,比如波特率I2cplck置位,i2c使能等等很多寄存器。芯片的使用手册每一个部分都有很多寄存器 要设置,怎么才能确保不漏呢???我以前写i2c的驱动都是参考别人的来改,自己写的时候一般都会有遗漏,大家是怎么做到不漏的?我现在工程上需要用到UART外设,需要设置哪些寄存器呢?????一定采纳,有没有什么窍门

采用系统库,逐个初始化,这也许是最好的。

或者将自己、他人写好的代码逐步形成自己的代码库,然后按需使用(例如只初始化部分内容)

例如我自己的USART应用

/*
Alita 2012-07-14 v1
Alita 2012-07-15 V2 根据对比模块进行了完整的测试
Title: 将所有的UART(UART4/5)和USART(1,2,3)合并到此处进行
统一管理
Param:无
Return:无
1、端口初始化
2、基本通讯功能
*/
void Full_USART_Configuration(void)
{
/*
1、初始化USART1
2、初始化USART2
3、初始化USART3
4、初始化UART4
*/
// 0、公共对象
USART_InitTypeDef USART_InitStructure; // USART对象结构
USART_ClockInitTypeDef  USART_ClockInitStructure; // 时钟结构
GPIO_InitTypeDef GPIO_InitStructure;   // 通用GPIO结构

// 1、初始化USART1=========================================
// 初始化USART TX端口(PA9)推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 初始化USART TX端口(PA10)浮空输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//
USART_ClockInitStructure.USART_Clock = 
USART_Clock_Disable;    //时钟低电平活动
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; //时钟低电平
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; //时钟第二个边沿进行数据捕获
USART_ClockInitStructure.USART_LastBit = 
USART_LastBit_Disable; //最后一位数据的时钟脉冲不从SCLK输出
/* Configure the USART1 synchronous paramters */
USART_ClockInit(USART1, &USART_ClockInitStructure); // 初始化时钟
//
USART_InitStructure.USART_BaudRate = USART_BRATES; // 波特率-默认USART_BRATES  测试9600,更高速度丢失数据
USART_InitStructure.USART_WordLength = 
USART_WordLength_8b; // 数据长度8
USART_InitStructure.USART_StopBits = USART_StopBits_1; // 停止位1
USART_InitStructure.USART_Parity = USART_Parity_No ; // 奇偶失能(不开启奇偶效验)
USART_InitStructure.USART_HardwareFlowControl = 
USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = 
USART_Mode_Rx | USART_Mode_Tx; // 接收使能|发送使能
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE); // 使能USART1

// 2、初始化USART2=========================================
// 初始化USART TX端口(PA2)推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 初始化USART TX端口(PA3)浮空输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//
USART_ClockInitStructure.USART_Clock = 
USART_Clock_Disable;    //时钟低电平活动
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; //时钟低电平
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; //时钟第二个边沿进行数据捕获
USART_ClockInitStructure.USART_LastBit = 
USART_LastBit_Disable; //最后一位数据的时钟脉冲不从SCLK输出
USART_ClockInit(USART2, &USART_ClockInitStructure); // 初始化时钟
//
USART_InitStructure.USART_BaudRate = USART_BRATES; // 波特率-默认USART_BRATES  测试9600,更高速度丢失数据
USART_InitStructure.USART_WordLength = 
USART_WordLength_8b; // 数据长度8
USART_InitStructure.USART_StopBits = USART_StopBits_1; // 停止位1
USART_InitStructure.USART_Parity = USART_Parity_No ; // 奇偶失能(不开启奇偶效验)
USART_InitStructure.USART_HardwareFlowControl = 
USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = 
USART_Mode_Rx | USART_Mode_Tx; // 接收使能|发送使能
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE); // 使能USART1

// 3、初始化USART3=========================================
// 初始化USART TX端口(PB10)推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// 初始化USART TX端口(PB11)浮空输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//
USART_ClockInitStructure.USART_Clock = 
USART_Clock_Disable;    //时钟低电平活动
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; //时钟低电平
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; //时钟第二个边沿进行数据捕获
USART_ClockInitStructure.USART_LastBit = 
USART_LastBit_Disable; //最后一位数据的时钟脉冲不从SCLK输出
USART_ClockInit(USART3, &USART_ClockInitStructure); // 初始化时钟
//
USART_InitStructure.USART_BaudRate = USART_BRATES; // 波特率-默认USART_BRATES  测试9600,更高速度丢失数据
USART_InitStructure.USART_WordLength = 
USART_WordLength_8b; // 数据长度8
USART_InitStructure.USART_StopBits = USART_StopBits_1; // 停止位1
USART_InitStructure.USART_Parity = USART_Parity_No ; // 奇偶失能(不开启奇偶效验)
USART_InitStructure.USART_HardwareFlowControl = 
USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = 
USART_Mode_Rx | USART_Mode_Tx; // 接收使能|发送使能
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE); // 使能USART1


// 4、初始化USART4=========================================
// 初始化USART TX端口(PC10)推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
// 初始化USART TX端口(PC11)浮空输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//
USART_ClockInitStructure.USART_Clock = 
USART_Clock_Disable;    //时钟低电平活动
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; //时钟低电平
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; //时钟第二个边沿进行数据捕获
USART_ClockInitStructure.USART_LastBit = 
USART_LastBit_Disable; //最后一位数据的时钟脉冲不从SCLK输出
USART_ClockInit(UART4, &USART_ClockInitStructure); // 初始化时钟
//
USART_InitStructure.USART_BaudRate = USART_BRATES; // 波特率-默认USART_BRATES  测试9600,更高速度丢失数据
USART_InitStructure.USART_WordLength = 
USART_WordLength_8b; // 数据长度8
USART_InitStructure.USART_StopBits = USART_StopBits_1; // 停止位1
USART_InitStructure.USART_Parity = USART_Parity_No ; // 奇偶失能(不开启奇偶效验)
USART_InitStructure.USART_HardwareFlowControl = 
USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = 
USART_Mode_Rx | USART_Mode_Tx; // 接收使能|发送使能
USART_Init(UART4, &USART_InitStructure);
USART_Cmd(UART4, ENABLE); // 使能USART1

}

同时重载fputc的时候使用了消息级别参数,来控制消息发送的通道,是USARTx还是全端口广播

// 重载了C语言库中的fputc函数,用于使用USART发送数据
int fputc(int ch, FILE *f)
{
switch(USARTChannel_Flag)
{
case 0:{}break;
case 1:
{
USART_SendData(USART1, (u8) ch);
while(USART_GetFlagStatus(
USART1, USART_FLAG_TC) == RESET){ }
}break;
case 2:
{
USART_SendData(USART2, (u8) ch);
while(USART_GetFlagStatus(
USART2, USART_FLAG_TC) == RESET){ }
}break;
case 3:
{
USART_SendData(USART3, (u8) ch);
while(USART_GetFlagStatus(
USART3, USART_FLAG_TC) == RESET){ }
}break;
case 4:
{
USART_SendData(UART4, (u8) ch);
while(USART_GetFlagStatus(
UART4, USART_FLAG_TC) == RESET){ }
}break;
return ch
}
}

 如此,只要这个代码完全通过了测试,那么以后就可以根据这个模板来修改,如果只用USART1,那么屏蔽剩下的

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-07-25
芯片的各个功能都是有很多设置位的,但是并不是说每个位都要设定。有的位不影响你的功能,有的位在特点应用场合是采用默认值。一般来说你看别人的程序怎么设置、设置了哪些,你照着做就行了本回答被提问者采纳
第2个回答  2013-07-26
去找找周立功的单片机例程吧~里面的例程基本都能用上。