Thursday, 1 March 2018

microcontroller - What is bit banging


I am new to microcontroller programming. I am using ATmega32-A controller and CodeVisionAVR compiler. I am using the waveform generator(AD9833) to generate a sinewave signal using SPI communication. I am able to generate the sinewave successfully. Now I am passing that signal to the sensor. The sensor output is selected through the multiplexer and sent to the ADC. Now I want read ADC values using SPI communication. I have tried a lot to setup the registers of ADC. Still it is not working. To see SPI communication Code have a look at my previous post ADC registers setup using spi communication. I am using USART(RS232) communication to print values on PC(PuTTY).


Someone advised me to use bit-banging. I am new to that concept. Can any one provide me an example code of bit banging of SPI communication. How to start that procedure? Can any one provide me a good materiel. Do I need any external hardware?


I have written this, including pin connections:


#define ADC_CS PORTB.3
#define MOSI PORTB.5

#define MISO PINB.6
#define SCK PORTB.7

void send_8bit_serial_data(unsigned char data)
{
int i;
ADC_CS=0;
for (i = 0; i < 8; i++)
{
// consider leftmost bit

// set line high if bit is 1, low if bit is 0
if (data & 0x80)
output_high(PORTB.5);
else
output_low(PORTB.5);

// pulse clock to indicate that bit value should be read
output_low(PORTB.7);
output_high(PORTB.7);


// shift byte left so next bit will be leftmost
data <<= 1;
}

// deselect device
ADC_CS=1;
}

Answer



Bit banging is creating the whole series of pulses in software, instead of relying on a piece of hardware inside the microcontroller.


Many microcontrollers have a hardware SPI, and then all you have to do is write a byte to the output register, and the SPI controller will shift the data out, and at the same time receive data from the slave. You can get an interrupt when the transfer is complete, and then read the received data.



But some microcontrollers don't have the SPI hardware on board and then you have to simulate it by doing everything manually. SPI has a number of different modes, I'll use this pulse diagram as an example:


enter image description here


So while a dedicated SPI controller takes care of all the pulses, data shifting and timing, when bit-banging you have to take every action yourself:


Make Slave Select low  
Short delay
Do 8 times
Make the SCK (Serial Clock) pin low
Make the MOSI (Master-Out-Slave-In) pin high or low depending on bit 7 of the data
Add brief delay
Make the SCK output high

Read MISO (Master-In-Slave-Out) pin
Shift received data left, and shift the bit just read in as bit 0
Add brief delay
Shift the data byte 1 bit left
Make Slave Select high again

Bit-banging SPI is relatively simple, the code for bit-banging I2C for instance will be more complex, and you'll need a timer somehow if you want to bit-bang the UART protocol.


No comments:

Post a Comment

arduino - Can I use TI&#39;s cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...