This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/TM4C123GH6PM: issue with bit shift implementation in code

Part Number: TM4C123GH6PM


Tool/software: TI C/C++ Compiler

Hi there,

I'm new to Tiva. I tried to write a code to generate a sine wave for sound generation in Embedded C. I've used the FPU to generate sound. I don't know the bit shift method which converts 32 bit value to 8 bit value. My code generates the value of 32 bit, i need to write 8 bit value. Anyone here would help me? 

Below is my code:

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include <math.h>
#include "driverlib/fpu.h"
#include "driverlib/debug.h"
#include "driverlib/pwm.h"
#include "driverlib/pin_map.h"

#ifndef M_PI

 

#define M_PI 3.14159265358979323846
#define PWM_FREQUENCY 55


#endif


#define SERIES_LENGTH 100

float gSeriesData[SERIES_LENGTH];


volatile uint32_t a =0;

volatile uint32_t b =0;

volatile uint32_t c =0;

volatile uint32_t d =0;

volatile uint8_t j =0;


int main(void)

{


    SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN); // set up the clock

 

   // SysCtlPWMClockSet(SYSCTL_PWMDIV_64);

 

   // SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);

 

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // enable port A for Switch

 

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // enable port B for DAC

 

    SysCtlDelay(3);

 

    //GPIOPinTypePWM(GPIO_PORTD   _BASE, GPIO_PIN_0);

 

    //GPIOPinConfigure(GPIO_PB0_M1PWM0);

 

    GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5);

 

    GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);

 

    GPIOPadConfigSet(GPIO_PORTA_BASE,GPIO_PIN_2,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); // Internal pull down resistor for pin2

 

    FPULazyStackingEnable();

 

    FPUEnable();

 

   // uint32_t ui32PinData;

 

    float fRadians;

 

    int32_t i32DataCount = 0;

 

    uint8_t i = 0;

 

    fRadians = ((2 * M_PI) / SERIES_LENGTH);

 

    while(1)
    {
        a = GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_2);


        if(a==0)


             {

               while(i32DataCount < SERIES_LENGTH)

               {

               gSeriesData[i32DataCount] = 100*sinf(fRadians * i32DataCount);

 

                 for (i=0; i<=100; i++)

 

               {

            //  j =  100*gSeriesData[i];

                  GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, gSeriesData[i]);

               }
               i32DataCount++;
               }

                i32DataCount =0;

             }

     }

}

 


Also, can anyone tell me if I'm wrong anywhere in implementation? I've referred the FPU code from : www.cse.iitb.ac.in/.../TM4C123G_LaunchPad_Workshop_Workbook.pdf

  • Hello Nikita,

    This forum is really for device specific questions and not C code implementation. These sorts of questions are best for sites like Stackoverflow.

    That said as you didn't know that I will answer this one question, but please keep future topics to device specific questions about our TM4C MCU and it's peripherals.

    To place a 32 bit output into an 8 bit variable, you have a few options. The quickest is if you know only the first 8 bits of data in the 32 bit variable are what you need. If this is the case, you can do a simple typecasting:

    ui8Variable1 = (uint8_t) ui32Variable2;

    If you need to pull out bits which are higher than the first 8 bits, you need to also apply bit shifting to shift the bits you want to be in the lowest 8 bits of the 32 bit variable before doing a typecast. For example if you have data like 0x87654321 in your 32 bit variable, and you want the 0x43 to be in your 8 bit variable, you have to bit shift right by 8 first and then type cast like this:

    ui8Variable1 = (uint8_t) (ui32Variable2 >> 8);

    Some links to read: https://www.cprogramming.com/tutorial/lesson11.html

    https://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm

    https://stackoverflow.com/questions/141525/what-are-bitwise-shift-bit-shift-operators-and-how-do-they-work

    Lastly for floating point, that's another topic entirely if you want to keep the decimals, and for that see my prior post about how to handle such a case: e2e.ti.com/.../2438264

    Hopefully this helps, best of luck!