概述
数据协议采用单线归零码的通讯方式,像素点在上电复位以后,DIN端接受从控制器传输过来的数据,首先送过来的24bit数据被第一个像素点提取后,送到像素点内部的数据锁存器,剩余的数据经过内部整形处理电路整形放大后通过DO端口开始转发输出给下一个级联的像素点,每经过一个像素点的传输,信号减少24bit;像素点采用自动整形转发技术,使得该像素点的级联个数不受信号传送的限制,仅受限信号传输速度要求;
定时器设置时间只用关心高电平时间。低电平时间不用太准确
整体思路:
定时器输出脉宽可调的方波,更新事件触发DMA将脉宽数据发送到比较寄存器。
1,RGB数据缓存: 定时器比较寄存器是16位,每一个方波占用16位。对齐问题用8位DMA会传输错误。1个灯24位颜色占用24*2字节,最后低电平占用一个周期加一位。
2.,将各灯的颜色安格式设置缓存,每次触发DMA就刷新一次颜色。
参考原码
#define LEDNUMSIZE 1 //灯的数量
unsigned short LEDBuf[LEDNUMSIZE*24+1]={0};//LED RGB 缓冲区 24字节一个灯 + 一字节0结束
unsigned char updateLED=0;
/* TIM1 init function */
void MX_TIM8_Init(void)
{
LL_TIM_InitTypeDef TIM_InitStruct = {0};
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
LL_TIM_BDTR_InitTypeDef TIM_BDTRInitStruct = {0};
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
/* Peripheral clock enable */
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM8);
/**TIM8 GPIO Configuration
PB8 ------> TIM8_CH2 //输出引脚
*/
GPIO_InitStruct.Pin = LL_GPIO_PIN_8;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
GPIO_InitStruct.Alternate = LL_GPIO_AF_10;
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
TIM_InitStruct.Prescaler = 1;
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
TIM_InitStruct.Autoreload = 100;
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
TIM_InitStruct.RepetitionCounter = 0;
LL_TIM_Init(TIM8, &TIM_InitStruct);
LL_TIM_DisableARRPreload(TIM8);
LL_TIM_OC_EnablePreload(TIM8, LL_TIM_CHANNEL_CH2);
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
TIM_OC_InitStruct.CompareValue = 0;
TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
TIM_OC_InitStruct.OCNPolarity = LL_TIM_OCPOLARITY_HIGH;
TIM_OC_InitStruct.OCIdleState = LL_TIM_OCIDLESTATE_LOW;
TIM_OC_InitStruct.OCNIdleState = LL_TIM_OCIDLESTATE_LOW;
LL_TIM_OC_Init(TIM8, LL_TIM_CHANNEL_CH2, &TIM_OC_InitStruct);
LL_TIM_OC_DisableFast(TIM8, LL_TIM_CHANNEL_CH2);
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_FROZEN;
LL_TIM_SetTriggerOutput(TIM8, LL_TIM_TRGO_RESET);
LL_TIM_SetTriggerOutput2(TIM8, LL_TIM_TRGO2_RESET);
LL_TIM_DisableMasterSlaveMode(TIM8);
//高级定时器要设置BDTR寄存器
TIM_BDTRInitStruct.OSSRState = LL_TIM_OSSR_DISABLE;
TIM_BDTRInitStruct.OSSIState = LL_TIM_OSSI_DISABLE;
TIM_BDTRInitStruct.LockLevel = LL_TIM_LOCKLEVEL_OFF;
TIM_BDTRInitStruct.DeadTime = 0;
TIM_BDTRInitStruct.BreakState = LL_TIM_BREAK_DISABLE;
TIM_BDTRInitStruct.BreakPolarity = LL_TIM_BREAK_POLARITY_HIGH;
TIM_BDTRInitStruct.BreakFilter = LL_TIM_BREAK_FILTER_FDIV1;
TIM_BDTRInitStruct.BreakAFMode = LL_TIM_BREAK_AFMODE_INPUT;
TIM_BDTRInitStruct.Break2State = LL_TIM_BREAK2_DISABLE;
TIM_BDTRInitStruct.Break2Polarity = LL_TIM_BREAK2_POLARITY_HIGH;
TIM_BDTRInitStruct.Break2Filter = LL_TIM_BREAK2_FILTER_FDIV1;
TIM_BDTRInitStruct.Break2AFMode = LL_TIM_BREAK_AFMODE_INPUT;
TIM_BDTRInitStruct.AutomaticOutput = LL_TIM_AUTOMATICOUTPUT_ENABLE;
LL_TIM_BDTR_Init(TIM8, &TIM_BDTRInitStruct);
//Tx DMA
MODIFY_REG(DMA1_Channel6->CCR,DMA_CCR_MSIZE_Msk,1<<DMA_CCR_MSIZE_Pos);//存储器数据大小
MODIFY_REG(DMA1_Channel6->CCR,DMA_CCR_PSIZE_Msk,1<<DMA_CCR_PSIZE_Pos);//外设数据大小
SET_BIT(DMA1_Channel6->CCR,DMA_CCR_MINC);//存储器递增
CLEAR_BIT(DMA1_Channel6->CCR,DMA_CCR_PINC);//外设固定
// SET_BIT(Tx2DMA->CCR,DMA_CCR_CIRC);//循环模式
MODIFY_REG(DMA1_Channel6->CCR,DMA_CCR_DIR_Msk,1<<DMA_CCR_DIR_Pos);//传输方向
DMA1_Channel6->CPAR=(int)&TIM8->CCR2;//外设地址
DMA1_Channel6->CMAR=(int)LEDBuf;//内存地址
MODIFY_REG(DMAMUX1_Channel5->CCR,DMAMUX_CxCR_DMAREQ_ID_Msk,53<<DMAMUX_CxCR_DMAREQ_ID_Pos);//邦定DMA通道与外设请求
LL_TIM_EnableDMAReq_UPDATE(TIM8); //使能定时器的DMA请求
LL_TIM_CC_EnableChannel(TIM8,LL_TIM_CHANNEL_CH2);//使能比较输出
LL_TIM_EnableCounter(TIM8);//使能定时器
}
//根据buf中的颜色设置LEDBuf数值,并启动DMA和定时器发送信号
//buf存放颜色 G0 R0 B0 G1 R1 B1 G2 R2 B2 .....
void LedUpdateRGB(unsigned char *buf,unsigned short n )
{
int s=0;
int i;
unsigned short *ptime=LEDBuf;
CLEAR_BIT(TIM8->CR1,TIM_CR1_CEN);//停止定时器
CLEAR_BIT(DMA1_Channel6->CCR,DMA_CCR_EN);//清DMA使能
while(s<n && n<=LEDNUMSIZE){
for(i=0;i<8;i++){ //G 绿色 8位
*ptime=(*buf&(0x80>>i)) ? 50 : 18; //脉宽设置 根据定时器时间
ptime++;
}
buf++;
for(i=0;i<8;i++){ //R
*ptime=(*buf&(0x80>>i)) ? 50 : 18;
ptime++;
}
buf++;
for(i=0;i<8;i++){ //B
*ptime=(*buf&(0x80>>i)) ? 50 : 18;
ptime++;
}
buf++;
s++;
}
*ptime=0;//最后加一个周期保持低电平
//如果触发不了 要删除DMA的标志位才能再次触发
DMA1_Channel6->CNDTR=LEDNUMSIZE*24+1; //DMA传输长度
SET_BIT(DMA1_Channel6->CCR,DMA_CCR_EN); //DMA使能
TIM8->CNT=0; //重新计时
SET_BIT(TIM8->CR1,TIM_CR1_CEN); //开始
}
总结:
DMA更新定时器输出,无等待时间。