1.任务描述
(1)ds18b20测量环境温度存储到存储器(数组)中。
(2)开启DMA将数组中的内容,通过DMA发送到串口
存在问题,ds18b20读到的数据是正常的,但是串口只是发送其低8位。
2.相关程序
2,1定义数据缓冲区
u8 SendBuff[SENDBUFF_SIZE*2]={0};
2.2 串口的配置
void Usart_Init(void)
{//定义结构体GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;// 开启IO口时钟DEBUG_USART_GPIO_APBxClkCmd(DEBUG_USART_GPIO_CLK, ENABLE);// 开启复用功能时钟DEBUG_USART_APBxClkCmd(DEBUG_USART_CLK, ENABLE);// GPIO的初始化GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_GPIO_PIN;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin = DEBUG_USART_RX_GPIO_PIN;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure);//串口的初始化USART_InitStructure.USART_BaudRate=9600; //²¨ÌØÂÊÉèÖÃΪ9600USART_InitStructure.USART_WordLength=USART_WordLength_8b;USART_InitStructure.USART_StopBits=USART_StopBits_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(DEBUG_USARTx, &USART_InitStructure); USART_Cmd(DEBUG_USARTx, ENABLE);
}
2.3 DMA的初始化
/***************** ·¢ËÍÒ»¸ö16λÊý **********************/
void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch)
{uint8_t temp_h, temp_l;/* È¡³ö¸ß°Ëλ */temp_h = (ch&0XFF00)>>8;/* È¡³öµÍ°Ëλ */temp_l = ch&0XFF;/* ·¢Ë͸߰Ëλ */USART_SendData(pUSARTx,temp_h); while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);/* ·¢Ë͵ͰËλ */USART_SendData(pUSARTx,temp_l); while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}///Öض¨Ïòc¿âº¯Êýprintfµ½´®¿Ú£¬Öض¨Ïòºó¿ÉʹÓÃprintfº¯Êý
int fputc(int ch, FILE *f)
{/* ·¢ËÍÒ»¸ö×Ö½ÚÊý¾Ýµ½´®¿Ú */USART_SendData(DEBUG_USARTx, (uint8_t) ch);/* µÈ´ý·¢ËÍÍê±Ï */while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_TXE) == RESET); return (ch);
}///Öض¨Ïòc¿âº¯Êýscanfµ½´®¿Ú£¬ÖØдÏòºó¿ÉʹÓÃscanf¡¢getcharµÈº¯Êý
int fgetc(FILE *f)
{/* µÈ´ý´®¿ÚÊäÈëÊý¾Ý */while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_RXNE) == RESET);return (int)USART_ReceiveData(DEBUG_USARTx);
}/*** @brief USARTx TX DMA ÅäÖã¬ÄÚ´æµ½ÍâÉè(USART1->DR)* @param ÎÞ* @retval ÎÞ*/
void USARTx_DMA_Init(void)
{DMA_InitTypeDef DMA_InitStructure;// ¿ªÆôDMAʱÖÓRCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);// ÉèÖÃDMAÔ´µØÖ·£º´®¿ÚÊý¾Ý¼Ä´æÆ÷µØÖ·*/DMA_InitStructure.DMA_PeripheralBaseAddr = USART_DR_ADDRESS;// ÄÚ´æµØÖ·(Òª´«ÊäµÄ±äÁ¿µÄÖ¸Õë)DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff;// ·½Ïò£º´ÓÄÚ´æµ½ÍâÉè DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;// ´«Êä´óС DMA_InitStructure.DMA_BufferSize = SENDBUFF_SIZE*2;// ÍâÉèµØÖ·²»Ôö DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;// ÄÚ´æµØÖ·×ÔÔöDMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;// ÍâÉèÊý¾Ýµ¥Î» DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;// ÄÚ´æÊý¾Ýµ¥Î»DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; // DMAģʽ£¬Ò»´Î»òÕßÑ»·Ä£Ê½DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; // ÓÅÏȼ¶£ºÖÐ DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; // ½ûÖ¹ÄÚ´æµ½ÄÚ´æµÄ´«ÊäDMA_InitStructure.DMA_M2M = DMA_M2M_Disable;// ÅäÖÃDMAͨµÀ DMA_Init(USART_TX_DMA_CHANNEL, &DMA_InitStructure); // ʹÄÜDMADMA_Cmd (USART_TX_DMA_CHANNEL,ENABLE);
}
3. 注意事项
由于温度数据超过了8位,串口的数据发送寄存器只有8位,所以应该将读取的温度的数值分低8位和高8位,分别存在两个数组元素中,具体代码如下:
for(i=0;i<40;i++){//读取温度temp=readtemp();//存储低8位SendBuff[i++] = temp&0xff;//存储高8位SendBuff[i++] = (temp&0XFF00)>>8;delay_ms(50);}//开启DMA请求USART_DMACmd(DEBUG_USARTx, USART_DMAReq_Tx, ENABLE);
4.实验现象:
读取数据的低8位是16进制数9b,低8位是01,所以综合起来是019b,根据ds18b20的读取规则,0x019b*0.0625=411*0.0625=25.6875℃,结果是正确的。