cancel
Showing results for 
Search instead for 
Did you mean: 

Need Help to understand this code

Asraful
Associate III

this is code from an Udemy tutorial but i couldn't get the part of 9 bit data transfer with no parity bit can anyone help me to understand that please

 

Screenshot 2023-10-12 195942.png

18 REPLIES 18
TDK
Guru

What part is tripping you up?

With 9 bit no parity, it uses (reads and advances) 2 bytes of the buffer per character.

If you feel a post has answered your question, please click "Accept as Solution".
Asraful
Associate III

can we talk in google meet then i will be able to explain my problem better it will be great help

 

Asraful
Associate III

suppose my data is uint8_t data[5]={0xFF, 0xFF, 0xFF, 0xFF, 0xFF} now the len of my data is 5 so it will be pass as len. Now my loop will iterate 5 times if i am not wrong but the problem is when we cast it into uint16_t *pdata it will take first two item as one now then it will be mask as 9 bits and pTxBuffer will be increment twice and point to next 16 bit. now my confusion is withing 3 iteration my data array will be completed and if we take 0xFFFF and mask it 
then the data become 0x1FF how they can be same

TDK
Guru

> suppose my data is uint8_t data[5]={0xFF, 0xFF, 0xFF, 0xFF, 0xFF}

This data is not 9-bit data.

9-bit data would be stored as an array of uint16_t values.

If you feel a post has answered your question, please click "Accept as Solution".
Asraful
Associate III

but what is saw in my pc if i cast 8bit array into 16 bit then it became from uint8_t data[5]={0xFF, 0xFF, 0xFF, 0xFF, 0xFF} to {0xFFFF, 0xFFFF, 0x00FF}

 

True but irrelevant. The issue comes from you using a uint8_t array of length 5 to pass 5 parameters of 2 bytes each. Clearly 5 != 10 so, as you mentioned in your previous post, the function will read outside of the bounds of the array.

So fix the problem: declare the array as uint16_t.

If you feel a post has answered your question, please click "Accept as Solution".
Asraful
Associate III

but in the tutorial i am following they are sending it as uint8_t and it is working properly thats why it is out of my brain

Asraful
Associate III

i literally want to talk with someone to clear the confusion i am still in confusion since last seven days still no solution i was able to found

Then find a tutor or teacher that you're paying. Perhaps find someone who can cover pointers, and data representation in memory

Do you want to send as 9-bit? If so that data has to come from somewhere.

You can't randomly cast a structure, and change it's content. You're just telling the compiler to process the data in memory a different way. The bytes in memory don't change, the size of the structure in bytes doesn't change.

Where the peripheral is adding the parity bit, it takes 8-bits from you and adds it into the 9-bit shift register. If you want to send 9-bits of actual data, it needs to read at least that much from memory, it reads 16-bits as that's the next size up sufficient to do that.

You could send an array of uint16_t

uint16_t data[5]={0x1FF, 0x1FF, 0x1FF, 0x1FF, 0x1FF}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..