PIN in Arduino = 0 does not trigger the motors - c

I am trying to use if statement to control the motors using C on the Arduino Uno board. However, when I set so that when the PIN, which is the sensor equals 0, the motors will run, it does not work at all. I also tried setting the PIN to not equal 0 and 1, both working fine. Can you guys give it a check? Thank you very much.
int MotorL=11;
int DirectionR=12;
int MotorR=10;
int DirectionL=13;
int SensorM=0;
#include <avr/io.h>
#include <util/delay.h>
void setup() {
pinMode(DirectionL,OUTPUT);
pinMode(DirectionR,OUTPUT);
pinMode(SensorM,INPUT);
digitalWrite(DirectionL,HIGH);
digitalWrite(DirectionR,HIGH);
analogWrite(MotorL,0);
analogWrite(MotorR,0);
}
void loop() {
if(PIND &_BV(PD6)==0){ //General movement: forward
analogWrite(MotorL,84);
analogWrite(MotorR,97);
}else{
analogWrite(MotorL,0);
analogWrite(MotorR,0);
}
}

== has higher precedence than &. You need to put the first two terms in parenthesis.
What you mean is:
if((PIND &_BV(PD6))==0)
But what the compiler sees is:
if(PIND & (_BV(PD6)==0))

Related

Pic 18f4550 capture mode

I would like to measure a pulse using the pic 18f4550 in capture mode, this pulse is generated by the pic microcontroller itself, for this I use a function which plays the role of the XOR logic gate (you find the function that I've used below). with RC0 and RC2 the inputs and RC6 the signal output. the pulse leaving RC6 enters ccp2 to be measured.
The problem I found is that the ccp2 cannot detect the impulse generated by the microcontroller. I don't know if there are any conditions to connect the pins of the microcontroller or something.
If anyone has an answer or a hint to fix this, I will be grateful!
and if you have any questions feel free to ask .thanks !!
UPDATE: I changed some instructions in the code, now the RC6 output provides a signal. but my LCD does not display anything. the RC6 output is present below.
UPDATE 2: the while(1) in the xor() function blocking the rest of my program, so the program never get out of xor() and my LCD wont display anything. when I don't use the while loop in xor () my RC6 produce anything, the same for the LCD.
I don't know where the problem is, I did everything in my power to find the bug . but the system still not working!!!
I will leave the program as it is, so new readers can understand what I am talking about.
#include <stdio.h>
#include <stdlib.h>
#include "osc_config.h"
#include "LCD_8bit_file.h"
#include <string.h>
unsigned long comtage,capt0,x;
char pulse[20];
char cosinus[20];
float period,dephTempo,deph,phi;
void init (){
IRCF0 =1; /* set internal clock to 8MHz */
IRCF1 =1;
IRCF2 =1;
PIE2bits.CCP2IE=1;
PIR2bits.CCP2IF=0;
CCPR2 =0; /*CCPR1 is capture count Register which is cleared initially*/
T3CONbits.RD16=1;
T3CKPS0=0;
T3CKPS1=0;
TMR3CS=0;
TMR3IF=0;
T3CCP2=0; /*Timer3 is the capture clock source for CCP2*/
}
void xor()
{
while(1)
{
if (PORTCbits.RC0==PORTCbits.RC2)
{
PORTCbits.RC6=0;
}
else if (PORTCbits.RC0!=PORTCbits.RC2)
{
PORTCbits.RC6=1;
}
}
}
void main()
{
TRISCbits.TRISC0=1;
TRISCbits.TRISC2=1;
TRISCbits.TRISC6=0;
xor();
LCD_Init();
while(1)
{
CCP2CON = 0b00000101;
PIR2bits.CCP2IF = 0;
TMR3ON = 0;
TMR3 = 0;
while (!PIR2bits.CCP2IF);
TMR3ON = 1;
CCP2CON = 0b00000100;
PIR2bits.CCP2IF = 0;
while (!PIR2bits.CCP2IF);
comtage = CCPR2;
dephTempo = (((float)comtage /30.518)/65536 );
sprintf(pulse,"%.3f ",dephTempo);
LCD_String_xy(0,0,"the pulse width is : ");
LCD_String_xy(2,9,pulse);
}
}

MPLAB infinite loop

I have 2 questions.
The first: I have a problem in the behavior of this code; when I run it in Proteus the program make flasher "repeat the code in the main function"
what should I do?
This is the code:
#include <p18f452.h>
#include <delays.h>
#include <io.h>
void main ()
{
TRISC=0x00;
PORTC=0xff;
Delay1KTCYx(900);
PORTC=0x00;
Delay1KTCYx(900);
while(1)
{
}
}
The second question: what is the proper delay function I can use? and how can I measure the delay time?
Is the watchdog disabled in simulation ? If it is enabled it will cause the repetition of the program.
Try adding this line after the includes.
#pragma config WDT = OFF
You only have code to generate one flash. Move the flash and delays into the loop:
for(;;)
{
PORTC = 0xff;
Delay1KTCYx(900);
PORTC = 0x00;
Delay1KTCYx(900);
}
Measuring roughly can be made manually by timing N flashes with a stopwatch. It's of course easier to use a measurement intrument (an oscilloscope is nice for this) if you have it.
Also, since your duty cycle is 50%, you can simplify the code:
PORTC = 0;
for (;;)
{
PORTC = ~PORTC;
Delay1KTCYx(900);
}
This uses bitwise not (~) to invert the bits of PORTC, which will make them toggle from one to zero and vice versa. Setting the entire port to 0 before the loop makes sure all pins are at a known state.

TIva C Series problems with I2C Interface

I'm currently trying to interface my Tiva C Series with a Sparkfun Breakout Board, IMU Digital Combo Board - 6 Degrees of Freedom ITG3200/ADXL345 and I'm having trouble with the I2C interface.
currently this is my code:
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/i2c.h"
uint8_t SLAVE_ADDRESS = 0x68;
uint32_t first_byte, second_byte, temperature, result;
void i2c_setup(void) {
//Enable the I2C Module
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C2);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
//Wait at least 5 clock cycles
SysCtlDelay(2);
//Configure SDA and SCL
GPIOPinConfigure(GPIO_PE4_I2C2SCL);
GPIOPinConfigure(GPIO_PE5_I2C2SDA);
//Wait at least 5 clock cycles
SysCtlDelay(2);
//Set PE4 as SCL
GPIOPinTypeI2CSCL(GPIO_PORTE_BASE, GPIO_PIN_4);
//Set PE5 as SDA
GPIOPinTypeI2C(GPIO_PORTE_BASE, GPIO_PIN_5);
//Configure Master,
I2CMasterInitExpClk(I2C2_BASE, SysCtlClockGet(), false);
}
uint32_t i2c_read() {
I2CMasterSlaveAddrSet(I2C2_BASE, SLAVE_ADDRESS, false);
I2CMasterDataPut(I2C2_BASE, 0x1A);
I2CMasterControl(I2C2_BASE, I2C_MASTER_CMD_SINGLE_SEND);
while(I2CMasterBusBusy(I2C2_BASE)); //Loop until the bus is no longer busy
I2CMasterSlaveAddrSet(I2C2_BASE, SLAVE_ADDRESS, true );
I2CMasterControl(I2C2_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
while(I2CMasterBusBusy(I2C2_BASE)); //Loop until the bus is no longer busy
first_byte = I2CMasterDataGet(I2C2_BASE);
return first_byte;
}
void setup()
{
Serial.begin(9600);
i2c_setup();
}
void loop()
{
int test = i2c_read();
Serial.println(test);
delay(1000);
}
I'm using Energia to test my program, and when I try to read from the specified register, I get the same result, no matter which register I choose, the result is always decimal 229 (this is the Accelerometer's Device Address).
Can somebody point me in the right direction, I've been looking at my code for quite some time and still don't know whats wrong...
Thanks!
I skimmed through your code and everything seems Okay. Clearly something is working right if you get a response. But Like Martin said , figuring the problem without being there is somewhat difficult. Instead of Writing 0x1A can you try using one of the other I2C commands for the accelerametor ? Also if the jumper is connected to VDD your address should be 0x69 (105 decimal) are you sure it's 0x68 ?
I looked up the documentation on sparkfuns website and they provided the following list
of commands.
char WHO_AM_I = 0x00;
char SMPLRT_DIV= 0x15;
char DLPF_FS = 0x16;
char GYRO_XOUT_H = 0x1D;
char GYRO_XOUT_L = 0x1E;
char GYRO_YOUT_H = 0x1F;
char GYRO_YOUT_L = 0x20;
char GYRO_ZOUT_H = 0x21;
char GYRO_ZOUT_L = 0x22;
GL hope everything works out. Been meaning to buy my own to play around with so keep me posted !

How to define a timer in avr-gcc

I am learning to use avr-gcc, but I have no idea, how to solve the following task:
The 8 bits from Port B should alternately set from 0 to 1 with an interval of 500 mili seconds.
I appreciate your help.
You can use #include <util/delay.h> , and if you write : _delay_loop_2(1000); you will have a delay of 1 ms;
You could use this function:
void delay()
{
for(int i=0;i<500;i++)
_delay_loop_2(1000);
}
Have a look at this example. This a very basic code for timer0:
#include<avr/io.h>
#include<avr/interrupt.h>
#define F_CPU 1000000UL
unsigned int t=0;
main()
{
DDRD=0xFF;
TCCR0=(1<<CS00);
TCNT0=0;
TIMSK=(1<<TOIE0);
sei();
while(1);
}
ISR(TIMER0_OVF_vect)
{
t++;
if(t==40000)
{
PORTD=~PORTD;
t=0;
}
}
As #Alex said you can #include <util/delay.h>, but instant of using the provided code (by #Alex) you can simple use _delay_ms(500);
This will provide you a delay of 500ms.
The choice is yours, just keep in mind that in both cases the frequency of your clock must be defined properly to your compiler:
Example for 16MHz:#define F_CPU 16000000UL

Microcontroller PIC16F877A c coding, to control an occupancy sensor and a photodiode at the same time

i am currently doing a prototype which combine 2 sensors, the PIR motion sensor by cytron.
and a photodiode (tiny, with 3 legs, with the model unknown)
The prototype works in a way that, when there is no light, and there is motion, the led will turn on.
Else, it will turn off.
I have wrote codes to test the both sensors separately, it works quite fine.
I face problem of the output of led when i combine the 2 codes.
It is as shown in below:
// include
//==========================================================================
# include <pic.h>
# include <htc.h>
// configuration
//==========================================================================
__CONFIG (0x3F32);
// define
//==========================================================================
#define sensor RB3
#define led RA5
#define led2 RB7
#define light RB5
#define _XTAL_FREQ 4000000
#define delay ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000.0)))
// main function
//==========================================================================
void main(void)
{
// unsigned long delay_time=5000;
TRISA = 0b00000000;
TRISB = 0b01111111; //Configure Port B as Input
ADCON1 =0B00000110;
led=0;
led2=0;
int i;
while(1) //Infinity Loop
{
if(!light)
{
if(sensor)
{
for(i=5;i>0;i--)
{
led2=0;
led=1;
__delay_ms(10000);
}
}
else if(!sensor)
{
if (i>0)
{ for(i=5;i>0;i--)
{
led2=0;
led=1;
__delay_ms(10000);
}
}
else if(i<=0)
{
led=0;
led2=1;
}
}
}
else if(light)
{
led=0;
led2=1;
}
}
}
I appreciate your help in advance.
Please help.
Thank you.
You declare the variable i but then don't initialize it to a value (I don't think the C compiler initializes it to 0 either, I am not sure). If that was not the case, then imagine the following scenario:, that in the the very first beginning of execution:
it was (!light) and (!sensor), it starts comparing i>0 or i<=0 but what is i initially??
you only assume that the if (sensor) body has executed at least once to give i an initial value. I don't know the details of your program requirements or flow, but I see this as an unsafe and a hidden bug.
While I'm no expert at the PIC micro's I wonder whether the method you are using to access the RAx and RBx is the appropriate one i.e. "led = 0", rather than "led &= ~(1 << led)" or something in like that.
You also seem to not initialize the "int i" variable which I assume would lead to issues with the if statements.
Best regards.
How about something like:
while (1)
{
led_on = 0;
if (!light && !sensor)
{
if (led_on == 0)
{
/* Turn on LED... */
led_on = 1;
}
}
else if (led_on == 1)
{
/* Turn off LED... */
led_on = 0;
}
}
when I was working on pic24 family using assembly language if I needed to assign 2 ports consecutively it turned out that the second assignment would not work(unless you are using tristate buffers...I don't know the exact reason but that was the case...).Anyway...what I'm trying to say is that try to give a few mseconds delay after each led value assignments like:
for(i=5;i>0;i--)
{
led2=0;
_delay_ms(50);
led=1;
__delay_ms(10000);
}

Resources