How to read a frame from hm01b0 image sensor? - image

I am attempting to connect the Arducam HM01B0 sensor to the nRF52840-DK embedded board using bare metal programming. The sensor has pins for a four-bit serial interface, and to use it, the user has to solder the pins. However, I am trying to make the HM01B0 work with the one-bit interface and capture an image.
I am able to read and write to the sensor registers over I2C. The datasheet presents the following timings characteristics to capture an image over a serial line.
I have connected the logic analyzer and I am able to see the corresponding waveform that is the data is being streamed over D0 line.
For 1 bit interface,the image sensor has a frame rate of 30 FPS and the frequency of PCLK is 36 Mhz. I know that GPIO pins would not be able to read the input signals in this
frequency range but I don't know how else to read a serial line. One option is making the image sensor as SPIM master and nrf52840 as SPIM slave. The image sensor provides the clock over PCLK which can be configured as SPI clock and the line D0 can be configured as MOSI line. However to read valid data bits/bytes over the serial line, VSYNC and HSYNC should also be read and made sure that their value is according to the serial video interface timings diagram. So I tried to read the serial line D0 by configuring it as a GPIO pin and I read 0x00 always.
How can I configure the image sensor and capture an image?
This is the code I have written,
// array to hold a complete frame
uint8_t frameBuffer[324][324];
#define PIN_VSYNC (28)
#define PIN_HSYNC (29)
#define PIN_PCLK (30)
#define PIN_D0 (31)
// initialise the pins of sensor
void hm0360_init()
{
// GPIO I/P
nrf_gpio_cfg_input(PIN_VSYNC, NRF_GPIO_PIN_PULLUP);
nrf_gpio_cfg_input(PIN_HSYNC, NRF_GPIO_PIN_PULLUP);
nrf_gpio_cfg_input(PIN_PCLK, NRF_GPIO_PIN_PULLUP);
// GPIO O/P
nrf_gpio_cfg_input(PIN_D0,NRF_GPIO_PIN_PULLUP);
for(int i = 0; i < 324 ; i++){
for(int j = 0; j < 324 ; j++){
frameBuffer[i][j] = 0;
}
}
}
void hm01b0_read_frame_bits()
{
int VSYNC_count = -1, HREF_count = 0;
int incomingBits;
unsigned char byte = 0;
int byteArrayIndex = 0;
int bitCount = 0;
while(nrf_gpio_pin_read(PIN_VSYNC) == LOW) { }
while(nrf_gpio_pin_read(PIN_VSYNC) == HIGH)
{
while(nrf_gpio_pin_read(PIN_HSYNC) == false && nrf_gpio_pin_read(PIN_VSYNC) == HIGH) { }
VSYNC_count++;
HREF_count = 0;
//Read a row
while(nrf_gpio_pin_read(PIN_HSYNC) == true)
{
while(nrf_gpio_pin_read(PIN_PCLK) == false) { }
// read 0 always
incomingBits = nrf_gpio_pin_read(PIN_D0);
byte = (byte << 1) | incomingBits;
bitCount++;
if (bitCount == 8) {
frameBuffer[VSYNC_count][HREF_count++] = byte;
bitCount = 0;
byte = 0;
}
if (VSYNC_count == 324) {
break;
}
while(nrf_gpio_pin_read(PIN_PCLK) == true) { }
}
}
}

Related

How to read multiple ADC channels using PIC24; can only get AN0

I am using a PIC24 to read data using 3 analog inputs but am only getting 1 to show the right result. I looked everywhere on the internet and am still not able to get the code to work.
I am trying to read 3 analog input signals and am only able to read in AN0.
I am using an accelerometer to obtain the data and show it on the LCD screen for now. I was able to implement 3 different ways to take the data and display it but only an0 works and an1 is not the right value.
void InitADC(int amask) {
AD1PCFG = 0xFFF8; // select AN0, AN1, AN2 as analog inputs
AD1CON1 = 0x00E0; // auto convert # end of sampling, Integer Data out.
// see Text pg. 179 & Sec. 17 on AD1CON1.
//AD1CON2bits.CSCNA = 1;
AD1CON3 = 0x1F01; // Tad = 2xTcy = 125ns. 31*Tad for conversion time.
//AD1CSSL = 0xFFF7; // Scan 3 channels
AD1CON1bits.ADON = 1; // Turn on the ADC
} // InitADC
main() {
InitADC(0xFFF8); // initialize the ADC and analog inputs
char x_string [12];
char y_string [12];
char z_string [12];
//TRISB = 1; // all PORTB pins as outputs
TRISBbits.TRISB0 = 1;
TRISBbits.TRISB1 = 1;
TRISBbits.TRISB2 = 1;
InitPMP(); // Initialize the Parallel Master Port
InitLCD(); // Initialize the LCD
float x_val;
float y_val;
float z_val;
float x_axis, y_axis, z_axis;
while (1) // main loop
{
x_axis= SelectPort(0);
x_val= ((((x_axis * 3.3)/ 1024)-1.58)/0.380);
sprintf(x_string, "X: %0.2f ", x_val);
ms_delay(2.5);
y_axis= SelectPort(1);
y_val= ((((y_axis * 3.3)/ 1024)-1.58)/0.380);
sprintf(y_string, "Y: %0.2f ", y_val);
ms_delay(2.5);
z_axis= SelectPort(2);
z_val= ((((z_axis * 3.3)/ 1024)-1.58)/0.380);
sprintf(z_string, "Z: %0.2f ", z_val);
ms_delay(2.5);
}
Here is the code where the data is read:
int SelectPort(int ch)
{
//int *result;
AD1CON1bits.ADON = 0; // Turn off the ADC to reconfigure
//result = &ADC1BUF0;
switch(ch) // set values based on the channel to use
{
case 0: // select AN0 as analog input
//AD1CHSbits.CH0SA=0;
//result = ADC1BUF0;
AD1PCFG = 0xFFFE;
break;
case 1:
//AD1CHSbits.CH0SA=1;
//result = ADC1BUF1;
AD1PCFG = 0xFFFD; // select AN1 as analog input
break;
case 2:
//AD1CHSbits.CH0SA=2;
AD1PCFG = 0xFFFB; // select AN2 as analog input
break;
// there's only so many options here, so there's not really a default case
}
AD1CON1bits.ADON = 1; // Turn on the ADC
AD1CHS = ch; // 1. select analog input channel
AD1CON1bits.SAMP = 1; // 2. Start sampling.
while (!AD1CON1bits.DONE); //5. wait for conversion to complete
AD1CON1bits.DONE = 0; // 6. clear flag. We are responsible see text.
return ADC1BUF0; // 7. read the conversion results
}
I am new to PIC24 and need help to figure out why I am not able to get multiple ADC channels to read the data.
You should always give some time for the ADC input voltage to settle after changing its input port. Also, make sure the impedance of your inputs signals are less than 10K.
Separating the ADC channel switching and reading its value will help, and give you an opportunity to add a delay.
I could not find AD1PCFG in the PIC24 datasheet. AD1PCFG is a PIC32 register... PIC24 uses ANSx and TRSx to set pins as analog inputs. Analog inputs should be set once, during the boot sequence. Feeding an analog signal to a digital input CMOS pin will result in increased power draw, and can even lead to hardware failure!
// Set the pins as analog inputs once and for all in your setup code.
void InitADC()
{
// select RB0/AN0, RB1/AN1, RB2/AN2 as analog inputs
TRISB |= 0x07;
ANSB |= 0x07;
// ...
}
void ADC_SelectInput(int ch)
{
AD1CHS = ch & 0x0F
}
int ADC_Read()
{
AD1CON1bits.SAMP = 1;
while (!AD1CON1bits.DONE)
;
AD1CON1bits.DONE = 0;
return ADC1BUF0;
}
Your loop then becomes:
// ...
int adc_in;
while (1)
{
ADC_SelectInput(0);
ms_delay(3); // settling delay, you could also do something
// else in the meantime instead of waiting.
// NOTE: the argument to ms_delay() is an integer
// so waiting at least 2.5 ms makes it 3.
// when computing floats, use float constants, not double.
x_val = (((ADC_Read() * 3.3f) / 1024) - 1.58f) / 0.380f;
sprintf(x_string, "X: %0.2f ", x_val);
ADC_SelectInput(1);
ms_delay(3);
y_val = (((ADC_Read() * 3.3f) / 1024) - 1.58f) / 0.380f;
sprintf(y_string, "Y: %0.2f ", y_val);
ADC_SelectInput(2);
ms_delay(3);
z_val = (((ADC_Read() * 3.3f) / 1024) - 1.58f) / 0.380f;
sprintf(z_string, "Z: %0.2f ", z_val);
}

PORTB's LEDs controlling via input PINA in avr

I'm trying to write a code in Atmel so I can control atmega32 PORTB's pins via switches connected
to PINA, it works but there is only one problem, if one switch is left high and an another switch
turns high the led connected(relates) to the second pin doesn't change, where did i go wrong?
#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 1000000UL //Setting the CPU frequency to 1MHz
toggle(void){ //Creating a toggle function to call it within the main function
unsigned char input = 0x00; //Creating an 8-bit variable so we can save the input port
int t = 1; //Loop variable
while(PINA != 0x00){
t = t + 1;
_delay_ms(1);
if(t == 100) break;
}
while(t != 100){
if((PINA != 0x00) && (input !=PINA))
{
input = PINA;
PORTB = PORTB^input;
t = t + 1;
_delay_ms(1);
}
else if(input == PINA)
{
t = t + 1;
_delay_ms(1);
}
else if(PINA == 0x00){
t = t + 1;
_delay_ms(1);
input = PINA;
}
}
}
int main(void)
{
DDRA = 0x00; //Setting PORTA as input
DDRB = 0xFF; //Setting PORTB as output
DDRC = 0xFF; //Setting PORTC as output
DDRD = 0x02; //Setting PORTD.2 as output
PORTB= 0x00; //Setting PORTB's output to zero
PORTC= 0x00; //Setting PORTC's output to zero
PORTD= 0x02; //Setting PORTD.2's output to one
while (1)
{
PORTD= 0x02; //Turning on the PORTD LED
toggle(); //Toggling the PORTB's LEDs
PORTD= 0x00; //Turning off the PORTD LED
toggle(); //Toggling the PORTB's LEDs
}
}
Analyze your code
look at this section
while(PINA != 0x00){
t = t + 1;
_delay_ms(1);
if(t == 100) break;
}
while(t != 100){
//your algorithm body processed here
}
if one of switches (which connected to PORTA) is left high then the first while loop condition PINA != 0x00 is always true the loop finish only when t==100, then, when you go to the second while Loop it will not excite because the condition t != 100 is already false
in other words your toggle() function will not process any data until you release all switches
solution
I have made a simple code for what you try to do
int main(void)
{
DDRA = 0x00; //Setting PORTA as input
DDRB = 0xFF; //Setting PORTB as output
DDRC = 0xFF; //Setting PORTC as output
DDRD = 0x02; //Setting PORTD.2 as output
PORTB= 0x00; //Setting PORTB's output to zero
PORTC= 0x00; //Setting PORTC's output to zero
PORTD= 0x02; //Setting PORTD.2's output to one
unsigned char currentInput=PINA;
unsigned char previousInput=PINA;
unsigned char changedBitsMusk = 0;
while (1)
{
currentInput = PINA; //update current reading
changedBitsMusk = currentInput ^ previousInput; // calculate what is changed from previous reading
if (changedBitsMusk) // if there is change happen
{
if (currentInput&changedBitsMusk) // only process the data if change is HIgh (i.e only when button is pressed)
{
PORTB^=changedBitsMusk; // toggle the corresponding pins
}
}
previousInput = currentInput;
}
}
When you are trying to control outputs according to another port inputs, you can simply do something like
PORTB = PINA;
Of course, if you don't need the full port you can always mask the bits you need from the entire port.
Also, this is not the way to do it if you don't have the same pin numbers paired in both ports (i.e.: if the pin 3 of one port controls the behavior of the pint 6 of the other, for example)
This way you can avoid a lot of ugly and complex logic
If you want to continue in the path you are going, try removing the "else" from the "else if"s, they are not needed and as far as I can see, the conditions are mutually exclusive

Blink LED without delay

So I'm creating a code so that I can read and store temperature values
for at least 8hours and store it in the Arduino EEPROM. I also want the Built in LED to flash once every second, while the temperature sensor records once every minute. I wrote the following but I'm left with the LED staying on for a whole minute then off for another minute and so on. I want it to keep constantly flashing. I know it's because of my delay(6000) but I don't know how to fix it. Any help is appreciated, thanks!
#include <EEPROM.h> //Librería para controlar la EEPROM de la Arduino
float tempC; //Initialize variable for storing Temperature
int tempPin = 0; //Conected at A0
int addr = 0; //Cantidad de espacios (bytes) iniciales
int count = 0; //counter needed to stop at a certain point before overflowing the EEPROM memory
int Voltages[501]; // Array with space for 501 data Points.A little over 8 hours. Holding integer values of Voltages.
float Temps[501]; //Holds float values for Temperatures
const int SWITCH = 8; //set switch to port 8
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // blink once per second
void setup(){
Serial.begin(115200);
pinMode(LED_BUILTIN,OUTPUT);
pinMode(SWITCH,INPUT_PULLUP);}
void loop(){
int i;
if (digitalRead(SWITCH)==HIGH & count<=501){
for (int i=0;i<EEPROM.length();i++){
EEPROM.write(i,0);} // Clears the EEPROM so that only new recorded values are shown. In case of terminating before 8h
Serial.println("-------Recording-------");
unsigned long currentMillis = millis(); // current time
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // save the last time the LED blinked
if (ledState == LOW) {// if the LED is off turn it on and vice-versa:
ledState = HIGH;} else {
ledState = LOW;}
digitalWrite(LED_BUILTIN, ledState);}
for (i=0; i<501; i++){
count = count+1;
int v = analogRead(tempPin); // reads voltage
Voltages[i] = v;
Serial.println(i);
Serial.println(Voltages[i]);
delay(60000);
EEPROM.put(addr, Voltages[i]); //stores voltages in EEPROM
addr= addr +2;}}
I think the solution you are looking for involves the use of timer interrupts. That would execute an interrupt service routine (might as well be a blinking led) regardless of what happens in the rest of your loop function. This might give you a better in-sight: Arduino timer interrupts

Smallest (code-wise) bit - banged serial interface on PIC10

I need to interface to a PIC10 micro with some serial interface. Since these small devices lack hardware support for SPI I2C and UART a Software solution is inevitable.
However, since I need to preserve as much of the Programm memory to store (static) configuration and identify information to be retrieved via said interface, what would probably be the smallest solution?
I will need to program this in ASM since there seems no good C compiler for PIC10. However, this will be my first real encounter with ASM to speak of.
Try this download which contains code samples and says:
"Listed below are five PIC UART software routines to use with PIC microprocessors that have no hardware UART"
Link rot has broken the original link try this link.
In my opinion the xc8 compiler works pretty well, also for the PIC10.
Here is an example in C:
#include <xc.h>
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000
#endif
#define Baudrate 1200 //bps
#define OneBitDelay (1000000/Baudrate)
#define DataBitCount 8 // no parity, no flow control
#define UART_RX RA1 // UART RX pin
#define UART_TX RA0 // UART TX pin
#define UART_RX_DIR TRISA1 // UART RX pin direction register
#define UART_TX_DIR TRISA0 // UART TX pin direction register
//Function Declarations
void InitSoftUART(void);
unsigned char UART_Receive(void);
void UART_Transmit(const char);
__CONFIG(FOSC_INTOSC & MCLRE_OFF & WDTE_OFF & LVP_OFF & CP_OFF &
WRT_OFF & PWRTE_OFF & WRT_OFF & BOREN_ON & LPBOR_ON & BORV_LO);
void main()
{
unsigned char ch = 0;
ANSELA = 0x00; // Set ports as digital I/O, not analog input
ADCON = 0x00; // Shut off the A/D Converter
FVRCON = 0x00; // Shut off the Voltage Reference
PORTA = 0x00; // Make all pins 0
InitSoftUART(); // Intialize Soft UART
InitSoftUART(); // Intialize Soft UART
while(1)
{
ch = UART_Receive(); // Receive a character from UART
UART_Transmit(ch); // Echo back that character
}
}
void InitSoftUART(void) // Initialize UART pins to proper values
{
UART_TX = 1; // TX pin is high in idle state
UART_RX_DIR = 1; // Input
UART_TX_DIR = 0; // Output
}
unsigned char UART_Receive(void)
{
// Pin Configurations
// GP1 is UART RX Pin
unsigned char DataValue = 0;
//wait for start bit
while(UART_RX==1);
__delay_us(OneBitDelay);
__delay_us(OneBitDelay/2); // Take sample value in the mid of bit duration
for ( unsigned char i = 0; i < DataBitCount; i++ )
{
if ( UART_RX == 1 ) //if received bit is high
{
DataValue += (1<<i);
}
__delay_us(OneBitDelay);
}
// Check for stop bit
if ( UART_RX == 1 ) //Stop bit should be high
{
__delay_us(OneBitDelay/2);
return DataValue;
}
else //some error occurred !
{
__delay_us(OneBitDelay/2);
return 0x000;
}
}
void UART_Transmit(const char DataValue)
{
/* Basic Logic
TX pin is usually high. A high to low bit is the starting bit and
a low to high bit is the ending bit. No parity bit. No flow control.
BitCount is the number of bits to transmit. Data is transmitted LSB first.
*/
// Send Start Bit
UART_TX = 0;
__delay_us(OneBitDelay);
for ( unsigned char i = 0; i < DataBitCount; i++ )
{
//Set Data pin according to the DataValue
if( ((DataValue>>i)&0x1) == 0x1 ) //if Bit is high
{
UART_TX = 1;
}
else //if Bit is low
{
UART_TX = 0;
}
__delay_us(OneBitDelay);
}
//Send Stop Bit
UART_TX = 1;
__delay_us(OneBitDelay);
}
Source for this code is the :Microchip forum

How to send integers via popen?

I need to be able to send an integer from cocoa to an arduino.
It is easy to send characters, i.e. single digit integers, but I can't seem to find a way of sending two and three digit integers.
The purpose of this is to control the brightness of an LED continuously from 0 to 255.
So far, I can either turn it on and off using the following code:
int ledPin = 9; // LED connected to digital pin 9
int incomingByte = 0; // for incoming serial data
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
if(incomingByte == 105){ //105 corresponds to i and is programmed in cocoa to turn the LED on
digitalWrite(ledPin, HIGH);
}
else if(incomingByte == 111){ //111 corresponds to o and is programmed in cocoa to turn the LED on
digitalWrite(ledPin, LOW);
}
}
}
However, I can't work out how to set values between 0 and 255. Instead of 'digitalWrite', I would use 'AnalogWrite', however, I don't know how to send the incomingByte to be a value between 0 and 255.
This is the cocoa code:
#import "MainController.h"
#implementation MainController
-(IBAction)ledOn:(id)sender{
popen("echo i > /dev/cu.usbmodem1411", "r");
}
-(IBAction)ledOff:(id)sender{
popen("echo o > /dev/cu.usbmodem1411", "r");
}
#end
Thanks.
You can treat the integers with more than one digits as cstring in your arduino code then convert them to integer via atoi().
The following code will capture the bytes from the serial buffer as cstring:
char buffer[MAX_BUFFER_SIZE]; // global c character array variable
boolean inputReady()
{
byte index = 0;
byte avail = Serial.available();
if(avail > 0)
{
while(index < avail)
{
byte val;
do
{
val = Serial.read();
}
while (val == -1); // if value is no longer -1, bytes are captured
buffer[index] = val;
index++;
}
buffer[index] = 0; //terminate the character array
return true;
}
return false;
}
Note: I prefer cstrings than the String class built in the arduino IDE since it uses more memory, but be careful using cstrings as they are prone to memory leaks if not managed well.
This is how your loop() block should look like:
void loop()
{
if(inputReady())
{
int pwmValue = atoi(buffer); // convert ascii buffer to integer.
if(pwmValue >= 0 && pwmValue <= 255) // filter values from 0-255
analogWrite(ledPin, pwmValue);
else
digitalWrite(ledPin, LOW); // turn off if pwmValue is more than 255
}
delay(100);
}
You can then send pwm values as string from your cocoa code via popen().
#import "MainController.h"
#implementation MainController
-(IBAction)ledPWMValue:(id)sender{
popen("echo 254 > /dev/cu.usbmodem1411", "r");
}
#end
I've tested this on my Arduino Uno and will probably work on other variants. I hope this helps you in your project and best of luck!

Resources