2016-09-26 01:41 AM
Dear All,
I need to make BCD 4 digit in 7-segment display.
My spi recieved data is 14 bit, take a look this sample code in another MCU.
void toBCD(int16 val,int8 * buff) {
//converts 16bit value, to five BCD digits. Tries to do it fairly
//efficiently, both in size, and speed.
int1 temp_bit;
int8 nibctr;
buff[0]=buff[1]=buff[2]=0;
do {
if ((buff[0] & 0xF)>=5) buff[0]+=3;
if ((buff[0] & 0xF0)>=0x50) buff[0]+=0X30;
if ((buff[1] & 0xF)>=5) buff[1]+=3;
if ((buff[1] & 0xF0)>=0x50) buff[1]+=0X30;
if ((buff[2] & 0xF)>=5) buff[2]+=3;
temp_bit=shift_left(&val,2,0);
shift_left(buff,3,temp_bit);
} while (val!=0);
}
void main() {
int16 val;
//three bytes for packed BCD result
int8 buff[3];
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
val=12345;
toBCD(val,buff);
//At this point, the three bytes will contain:
// 45
// 23
// 01
while (true);
}
1. How can I implement this idea in RTC application?
2. Is it possible to apply this link ..http://embedded-lab.com/blog/stm32s-internal-rtc/
Kindly help me.
Refference code is required.
2016-10-26 05:43 AM
In the 21st century it still simplifies external logic.
If output is needed in decimal, it is a lot easier to contain each digit in the 0-9 range than have it as 8 or 16-bit binary and decompose it into decimal digits later. ie avoids division and multiplication, which are complicated/expensive at a gate level.2016-10-26 09:00 AM
If you don't care about run time that's the straight forward way. For a project where the task of converting was time critial I searched and found a nice algo at http://www.oocities.org/v_d_d/gba/Bin2BCD.s.txt. Please note that this Game Boy Advanced code is ARM code, I ported that to to thumb and optimized a little bit more.
With best regards, Torsten2016-10-26 09:46 AM
If you don't care about run time that's the straight forward way.
I was going for brutal clarity, in my opinion this is ''just math'', and the simpler it is to demonstrate the quicker we can get to the more complex and efficient methods.I do like the example snippet, ARM had a whole cook-book of these types of brutally efficient shortcuts.2016-10-26 07:47 PM
Dear Sir Clive,
My company just give me old version code for this project, which has been run/debug/test in coocox IDE.I found nothing to make binary to BCD here.I feel this whole device code is most time consuming because,1. Key driver H and C files.( made for press keys)2. State files for seven segment, main state and global.3. There is nothing in SPI data converted to BCD, but in seven segment state file void sevenseg_val_set(uint32 value) function has been used.Now, what would be my new work, I dont understand. They did not use DMA, but flash code has made.2016-10-26 08:16 PM
I'm not sure what you expect me to do, this isn't my job/responsibility.
This code looks dysfunctional : return (pDataRx[i] << 24) | (pDataRx[i] << 16) | (pDataRx[i] << 8) | pDataRx[i]; The variable i isn't set, and presumably you don't want to repeat the same 8-bits throughout a 32-bit word. Not sure of the endian order of the SPI data here, but something like 0..3 or 3..0 would seem more appropriate. return (pDataRx[0] << 24) | (pDataRx[1] << 16) | (pDataRx[2] << 8) | pDataRx[3];2016-10-26 08:29 PM
https://datasheets.maximintegrated.com/en/ds/MAX31855.pdf
Returns 14-bit signed binary number in +8191/-8192 range So presumably want to take that number in as binary and crack it into 4 digits that you sequence to the display. The 7-segments are attach to the same GPIO bank, you could presumably cycle the digit display using a circular DMA (memory to GPIO), triggered by a TIM2016-10-26 09:44 PM
Dear Sir Clive,
Yes, the SPI protocol of MAX31855 is simple. Drive CS low and apply a clock signal at SCK to read the results at SO. Drive CS low to output the first bit on the SO pin.May be thinking that idea this code is implemented, take a look please, #include ''spi.h''#include ''gpio.h''SPI_HandleTypeDef hspi1;/* SPI1 init function */void MX_SPI1_Init(void){ hspi1.Instance = SPI1; hspi1.Init.Mode = SPI_MODE_MASTER; hspi1.Init.Direction = SPI_DIRECTION_2LINES_RXONLY; hspi1.Init.DataSize = SPI_DATASIZE_14BIT; // 14bit ##### hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; hspi1.Init.NSS = SPI_NSS_HARD_OUTPUT; //hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi1.Init.TIMode = SPI_TIMODE_DISABLE; hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; hspi1.Init.CRCPolynomial = 7; hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE; if (HAL_SPI_Init(&hspi1) != HAL_OK) { Error_Handler(); }}void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle){ GPIO_InitTypeDef GPIO_InitStruct; if(spiHandle->Instance==SPI1) { /* USER CODE BEGIN SPI1_MspInit 0 */ /* USER CODE END SPI1_MspInit 0 */ /* Peripheral clock enable */ __HAL_RCC_SPI1_CLK_ENABLE(); /**SPI1 GPIO Configuration PA15 ------> SPI1_NSS PB3 ------> SPI1_SCK PB4 ------> SPI1_MISO */ GPIO_InitStruct.Pin = TEMP__CS_Pin; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Alternate = GPIO_AF0_SPI1; HAL_GPIO_Init(TEMP__CS_GPIO_Port, &GPIO_InitStruct); GPIO_InitStruct.Pin = TEMP_SCK_Pin|TEMP_SO_Pin; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Alternate = GPIO_AF0_SPI1; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* USER CODE BEGIN SPI1_MspInit 1 */ /* USER CODE END SPI1_MspInit 1 */ }}void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle){ if(spiHandle->Instance==SPI1) { /* USER CODE BEGIN SPI1_MspDeInit 0 */ /* USER CODE END SPI1_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_SPI1_CLK_DISABLE(); /**SPI1 GPIO Configuration PA15 ------> SPI1_NSS PB3 ------> SPI1_SCK PB4 ------> SPI1_MISO */ HAL_GPIO_DeInit(TEMP__CS_GPIO_Port, TEMP__CS_Pin); HAL_GPIO_DeInit(GPIOB, TEMP_SCK_Pin|TEMP_SO_Pin); }Should I use this code again?May be in my code you are talking the variable calling, that was uint8_t aTxBuffer[] = ''**** SPI Message ****'';static uint16_t Buffercmp(uint8_t *pBuffer1, uint8_t *pBuffer2, uint16_t BufferLength);static uint16_t Buffercmp(uint8_t *pBuffer1, uint8_t *pBuffer2, uint16_t BufferLength);Could you kindly, show me a code that using circular DMA with TIM trigger?2016-11-04 12:40 AM
2016-11-04 08:33 AM
I'm not working with HAL/CubeMX
2016-11-06 08:11 PM