Bitwise operations on a long piece of memory [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Is it possible to do bitwise operations directly on a long piece of memory (say, 512 bit or even more)?
By directly I mean, NOT multiple shifting and watching bounds... as following code.
/*
// shifting 128 bit data [pc2, pc1] to left,
// and then assigning it to [rc2, rc1]:
*/
uint64_t au1, pc1, pc2, rc1, rc2;
rc1 = pc1 >> 63;\
au1 = pc2 << 1;\
rc2 = rc1 ^ au1;\
rc1 = pc1 << 1;\

Related

What is the difference between = and |= in C? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm new to C and I am trying to understand the difference between
DDRB |= 0b00000001;
and
DDRB = 0b00000001;
Do the two lines have same effect of writing one to data register B ?
How are they different?
Just could register google search for "|" ..so needed some help in understanding it.
The first operation is called bitwise OR | and it works as follows imagining the numbers A=200 and B=184, which in binary will be, 11001000 and 10111000, respectively. The operator "|" will compare bit a bit of each of those numbers, and returns 1 when either of the bits is 1, 0 otherwise. So in your case:
11001000
| 10111000
--------
= 11111000
the result will be 248 (11111000 in binary). Therefore DDRB |= 0b00000001; is DDRB = DDRB | 0b00000001.
The second operation (e.g., DDRB = 0b00000001) is just an assignment of a value to a variable.
Do the two lines have same effect of writing one to data register B ?
No.
DDRB = 0b00000001;
writes one to bit-0 and zero to bits 1 to 7, while:
DDRB |= 0b00000001;
is a read-modify-write operation equivalent to:
DDRB = DDRB | 0b00000001;
so it writes a one to bit-0 as before and leaves all other bits unchanged.
So for example if DDRB's current value were 0b11110000 after:
DDRB = 0b00000001;
it will be 0b00000001 while after:
DDRB |= 0b00000001;
it will be 0b11110001.
So one sets DDRB to 1, while the other sets DDRB:Bit-0 to 1.

STM32 HAL_Delay TIMER Microcontroller [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
What is the difference between HAL_Delay() function and an empty for-loop? Timer should create interrupt and switch off LED.
If I use HAL_Delay() in interrupt function the result is that LED is off forever:
void TIM6_DAC_IRQHandler() {
HAL_TIM_IRQHandler(&htim6);
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
HAL_Delay(125);
}
But if I use instead:
void TIM6_DAC_IRQHandler() {
HAL_TIM_IRQHandler(&htim6);
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
for (int i=0; i<1000000; i++);
}
then LED1, which is always on in main-file, is set off for short time and then on, as I expect.
So why the code with HAL_Delay does not work?
The rule of thumb: NEVER USE DELAYS in the interrupt handlers.
HAL_Delay uses SysTick interrupt and if the priority of SysTick is lower than the priority of the interrupt in which handler it is called, will end up in the dead loop as SysTick Handler will be never invoked.
empty loop:
I would personally advice to use another forms of the loops:
for(volatile count = 0; count < 1000; count++);
or
for(count = 0; count < 1000; count++) asm("");
https://godbolt.org/z/hY117n

Avr Problem: in spi, spdr register is 8 bit but my number is 16 bit [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I work with atmega16, in both side master and slave.
I want to send a number between 100-999 that entered by keypad to the slave.
Numbers are 16 bit, but spdr register is 8 bit, so I send first lsb then msb bits. This works well and I send my number correctly.
But my problem is in slave side:
I enabled interrupt spie in slave side so each spdr received from master took as a interrupt and goes to ISR function. But because of my number is 16 bit, I want to take each 16 bit or two byte as a interrupt. What should i do?
You know your message length (16 bit or 2 byte). Just create a ring buffer to store two bytes and fill the ring buffer. Maybe you can add a third byte as start or end byte or something else but this is up to you. Additional you set a flag if your transmission ends.
So your code can look like this. Note this code is only the ISR and for an XMega as SPI slave but it should help to understand the procedure.
#define SPI_BUFFER_SIZE 2
uint8_t SPI_RxSlaveBuffer[SPI_BUFFER_SIZE];
typedef struct
{
uint8_t* RxBuffer;
uint8_t BytesProcessed;
uint8_t Status;
} SPI_Buffer_t;
static SPI_Buffer_t SlaveBuffer;
int main()
{
// Some code
SlaveBuffer.RxBuffer = SPI_RxSlaveBuffer;
// Some other code
}
ISR(SPIC_INT_vect)
{
SlaveBuffer.RxBuffer[SlaveBuffer.BytesProcessed++] = SPIC.DATA;
if(SlaveBuffer.BytesProcessed >= SPI_BUFFER_SIZE - 1)
{
SlaveBuffer.BytesProcessed = 0x00;
SlaveBuffer.Status = 0x01;
}
}
You can also check the state of the SS pin and reset the counter if the pin is asserted by the master (in case that the master has aborted the transmission or something else) - for example with polling the SS pin or wiring the signal of the SS pin to an interrupt pin to generate an additional I/O interrupt.

Hardware Random Number Generator error Arm M4 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I´m new in Arm microcontroller programming. I read the datasheet and found this :
Reset/initialize.
Write 1 to CR[INTM], CR[HA], and CR[GO]
Poll SR[OREG_LVL] until it is not 0.
When SR[OREG_LVL] is not 0, read the available random data from
OR[RANDOUT]
Repeat steps 3 and 4 as needed.
These are the steps for generating random numbers. Could someone give me a code example ? I'm working with the k64 sub-family, specifically MK64FX512VLL12.
Here's what I've tried:
void Rng_Test()
{
RNG->CR |= RNG_CR_SLP_MASK;
RNG->CR |= RNG_CR_GO_MASK;
RNG->CR |= RNG_CR_HA_MASK;
RNG->CR |= RNG_CR_INTM_MASK;
while (RNG_SR_OREG_LVL_MASK == 0) { }
}
Using the code you already have
void Rng_Init() {
RNG->CR |= RNG_CR_SLP_MASK;
RNG->CR |= RNG_CR_GO_MASK;
RNG->CR |= RNG_CR_HA_MASK;
RNG->CR |= RNG_CR_INTM_MASK;
}
int Rng_GetRand() {
while ((RNG->SR & RNG_SR_OREG_LVL_MASK) == 0);
return RNG->OR;
}
Something like the above should work, Rng_Init() is required once, Rng_GetRand should return a new random number (not necessarily different) each time it's called.

C8051f312 microcontroller [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm not very good at C language, but I have write a very simple code to a C8051F312 microcontroller.
My code doesn't working. Please help me what did I wrong.
#include C8051F310.h
#include stdio.h
sbit LED_16 = P1^7; // green LED: 1 = ON; 0 = OFF
void init(void)
{
// XBRN registers_init
XBR0 = 0x00;
XBR1 = 0x00; // Enable the crossbar
PCA0MD = 0X00;
// port_init
P0MDOUT = 0x00; // Output configuration for P0
P1MDOUT = 0x40; // Output configuration for P1
P2MDOUT = 0x00; // Output configuration for P2
P3MDOUT = 0x00; // Output configuration for P3
}
void main(void)
{
init();
while (1)
{
LED_16 = 1; // LED continuously illuminated
}
}
1.First of all you should use one of 2 following options for #include directive
#include "path-spec"
#include <path-spec>
, not #include path-spec, as you did
2.To configuire 7th bit of P1 general I/O port to work in push-pull mode you should set
P1MDOUT = 0x80;
, not
P1MDOUT = 0x40;

Resources