int main(void)
{
/* Stop WDT */
MAP_WDT_A_holdTimer();
/* Selecting P1.2 and P1.3 in UART mode */
MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);
/* Setting DCO to 12MHz */
CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_12);
/* Configuring UART Module */
MAP_UART_initModule(EUSCI_A0_BASE, &uartConfig);
/* Enable UART module */
MAP_UART_enableModule(EUSCI_A0_BASE);
/* Configuring GPIO2.4 as peripheral output for PWM and P6.7 for button
* interrupt */
MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P2, GPIO_PIN4,
GPIO_PRIMARY_MODULE_FUNCTION);
redirect();
/* Configuring P1.0 as output */
MAP_GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0);
MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0);
/* Configuring Timer_A to have a period of approximately 500ms and
* an initial duty cycle of 10% of that (3200 ticks) */
//MAP_Interrupt_enableSleepOnIsrExit();
MAP_Interrupt_enableInterrupt(INT_TA0_0);
MAP_Timer_A_generatePWM(TIMER_A0_BASE,&pwmConfig);
MAP_Timer_A_clearInterruptFlag(TIMER_A0_BASE);
MAP_Timer_A_enableInterrupt(TIMER_A0_BASE);
MAP_Timer_A_enableCaptureCompareInterrupt
(TIMER_A0_BASE,TIMER_A_CAPTURECOMPARE_REGISTER_0);
/* Enabling MASTER interrupts */
MAP_Interrupt_enableMaster();
/* Sleeping when not in use */
while (1)
{
//MAP_PCM_gotoLPM0();
}
}
const int bit_length = 33;
int period;
int times[33];
int values[32];
x=598;
number_bit=10;
// this function - period,times,values
// x is the 32 bit integer , number_bit is how many bits in that integer
void int_To_Arr(uint32_t x,int number_bit){
int i = 0;
period = BIT_LENGTH * 67 ; // 15fps -> 1/15=66.67m
for (i = 0; i < number_bit ; i++) {
if (((x >> i) & 1) == 0) /* shift right by i-bits, check on/off */
values[i] = 1000; /* assign to values[i] based on result */
else
values[i] = 11000;
times[i] = BIT_LENGTH * i; /* set times[i] */
}
times[i] = BIT_LENGTH * i;
}
void TA0_0_IRQHandler(void)
{
int i,value;
MAP_Timer_A_clearCaptureCompareInterrupt(TIMER_A0_BASE,
TIMER_A_CAPTURECOMPARE_REGISTER_0);
time=time+1;
if(time>=period){
time=0;
MAP_GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN0);
}
for(i=0;times[i]!=-1;i++){
if(times[i]>time){
break;
}
value=values[i];
}
MAP_Timer_A_setCompareValue(TIMER_A0_BASE,TIMER_A_CAPTURECOMPARE_REGISTER_1, value);
}
So this function will take x (32-bit integer) and number_bit(how many bits in the integer) and will fill the array in the main. if the bit is 1 , values = 11000 which will turn on the led. if the bit is 0 ,values =1000 .Unfortunately, the LED doesnt blink or do anything. Before this I do it manually, yes the LED blinking.
int time=0;
const int BIT_LENGTH = 33;
int period;
int times[33]; //{x}
int values[32];//{y}
//new 1001010110
const int period=667; //15fps - bitlength*67
const int times[]={0,67,200,267,333,400,467,600,667,-1};
const int values[]={11000,1000,11000,1000,11000,1000,11000,1000};
If through the extended discussions we have had, you are seeking to fill the values
array based on the number_bits
in the uint32_t x
value passed to your int_to_array
function by shifting the value x
to the right number_bits
times and at each iteration determining whether that bit in x
is 0
or 1
and setting values[i] = 1000
if the bit is 0
and to 11000
if the bit is 1
, then you could do something like the following:
#define BIT_LENGTH 33 /* if you need a constant, define one (or more) */
#define FPS_MULT 33
...
/* fill values based on nbits bit-values in x,
* fill times based on BIT_LENGTH and index.
* note: CHAR_BIT defined in limits.h
* (defined as 8 for virtually all common systems)
*/
void int_to_array (uint32_t x, int nbits)
{
int i = 0; /* loop variable - can be declared in loop for C99+ */
period = BIT_LENGTH * FPS_MULT; // your 30fps-1/30=0.033
/* validate nbits <= 32 */
if (nbits > (int)(sizeof x * CHAR_BIT)) {
fprintf (stderr, "error: nbits out of range of uint32_t\n");
return;
}
for (i = 0; i < nbits; i++) {
if (((x >> i) & 1) == 0) /* shift right by i-bits, check on/off */
values[i] = 1000; /* assign to values[i] based on result */
else
values[i] = 11000;
times[i] = BIT_LENGTH * i; /* set times[i] */
}
times[i] = BIT_LENGTH * i; /* final times[BIT_LENGTH - 1] */
}
While I am still unclear where those values come from, based on our discussion, that should be what you are looking for. Otherwise, I'm still uncertain. note I have added a validation check to insure nbits
cannot exceed 32
(the number of bits in x
)
Validation Test
If you wanted to write a short bit of validation code for your function, you could just write a short program that passes the value provided on the command line as the first argument to your function as x
(nbits
won't change, it will always be sizeof x * CHAR_BIT
). The following code passes the first argument to the function (passing 10
by default if no argument is given on the command line)
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <limits.h>
#define BIT_LENGTH 33 /* if you need a constant, define one (or more) */
#define FPS_MULT 33
int period;
int times[BIT_LENGTH];
int values[BIT_LENGTH - 1];
/* fill values based on nbits bit-values in x,
* fill times based on BIT_LENGTH and index.
* note: CHAR_BIT defined in limits.h
* (defined as 8 for virtually all common systems)
*/
void int_to_array (uint32_t x, int nbits)
{
int i = 0; /* loop variable - can be declared in loop for C99+ */
period = BIT_LENGTH * FPS_MULT; // your 30fps-1/30=0.033
/* validate nbits <= 32 */
if (nbits > (int)(sizeof x * CHAR_BIT)) {
fprintf (stderr, "error: nbits out of range of uint32_t\n");
return;
}
for (i = 0; i < nbits; i++) {
if (((x >> i) & 1) == 0) /* shift right by i-bits, check on/off */
values[i] = 1000; /* assign to values[i] based on result */
else
values[i] = 11000;
times[i] = BIT_LENGTH * i; /* set times[i] */
}
times[i] = BIT_LENGTH * i; /* final times[BIT_LENGTH - 1] */
}
int main (int argc, char **argv) {
unsigned long tmp = argc > 1 ? strtoul (argv[1], NULL, 0) : 10;
uint32_t x;
int i, nbits = sizeof x * CHAR_BIT;
if (errno || tmp > UINT32_MAX) {
fprintf (stderr, "error: conversion error or value out of range.\n");
return 1;
}
x = (uint32_t)tmp;
int_to_array (x, nbits);
printf ("x: %u\n\nperiod: %d\n\n", x, period);
for (i = 0; i < nbits; i++)
printf ("values[%2d]: %5d times[%2d]: %5d\n",
i, values[i], i, times[i]);
printf (" times[%2d]: %5d\n", i, times[i]);
return 0;
}
(note: I tweaked the function by addition optional parenthesis to a check to get rid of a -pedantic
signed/unsigned comparison warning and added a final times[i] = BIT_LENGTH * i;
after the loop to handle times
having 1 more element than values
)
Example Test
Simple default value of 10 (1010)
$ ./bin/values_times
x: 10
period: 1089
values[ 0]: 1000 times[ 0]: 0
values[ 1]: 11000 times[ 1]: 33
values[ 2]: 1000 times[ 2]: 66
values[ 3]: 11000 times[ 3]: 99
values[ 4]: 1000 times[ 4]: 132
values[ 5]: 1000 times[ 5]: 165
values[ 6]: 1000 times[ 6]: 198
values[ 7]: 1000 times[ 7]: 231
values[ 8]: 1000 times[ 8]: 264
values[ 9]: 1000 times[ 9]: 297
values[10]: 1000 times[10]: 330
values[11]: 1000 times[11]: 363
values[12]: 1000 times[12]: 396
values[13]: 1000 times[13]: 429
values[14]: 1000 times[14]: 462
values[15]: 1000 times[15]: 495
values[16]: 1000 times[16]: 528
values[17]: 1000 times[17]: 561
values[18]: 1000 times[18]: 594
values[19]: 1000 times[19]: 627
values[20]: 1000 times[20]: 660
values[21]: 1000 times[21]: 693
values[22]: 1000 times[22]: 726
values[23]: 1000 times[23]: 759
values[24]: 1000 times[24]: 792
values[25]: 1000 times[25]: 825
values[26]: 1000 times[26]: 858
values[27]: 1000 times[27]: 891
values[28]: 1000 times[28]: 924
values[29]: 1000 times[29]: 957
values[30]: 1000 times[30]: 990
values[31]: 1000 times[31]: 1023
times[32]: 1056
or a larger value, 0xdeadbeef (11011110101011011011111011101111)
$ ./bin/values_times 0xdeadbeef
x: 3735928559
period: 1089
values[ 0]: 11000 times[ 0]: 0
values[ 1]: 11000 times[ 1]: 33
values[ 2]: 11000 times[ 2]: 66
values[ 3]: 11000 times[ 3]: 99
values[ 4]: 1000 times[ 4]: 132
values[ 5]: 11000 times[ 5]: 165
values[ 6]: 11000 times[ 6]: 198
values[ 7]: 11000 times[ 7]: 231
values[ 8]: 1000 times[ 8]: 264
values[ 9]: 11000 times[ 9]: 297
values[10]: 11000 times[10]: 330
values[11]: 11000 times[11]: 363
values[12]: 11000 times[12]: 396
values[13]: 11000 times[13]: 429
values[14]: 1000 times[14]: 462
values[15]: 11000 times[15]: 495
values[16]: 11000 times[16]: 528
values[17]: 1000 times[17]: 561
values[18]: 11000 times[18]: 594
values[19]: 11000 times[19]: 627
values[20]: 1000 times[20]: 660
values[21]: 11000 times[21]: 693
values[22]: 1000 times[22]: 726
values[23]: 11000 times[23]: 759
values[24]: 1000 times[24]: 792
values[25]: 11000 times[25]: 825
values[26]: 11000 times[26]: 858
values[27]: 11000 times[27]: 891
values[28]: 11000 times[28]: 924
values[29]: 1000 times[29]: 957
values[30]: 11000 times[30]: 990
values[31]: 11000 times[31]: 1023
times[32]: 1056
Look things over and let me know if you have further questions.