I am a newbie in arm programming. I am using teensy 3.2 which has MK20DX256 microcontroller chip and the led is connected to PTC5 of MC.
I have written this code to blink the LED.
The project builds without error but HEX file does not blink led. I am using teensy loader to program the board with HEX file.
Here is my code:
#include "MK20D7.h"
void delay(unsigned int);
int main(void) {
SystemCoreClockUpdate(); /* Get Core Clock Frequency */
SysTick_Config(SystemCoreClock/1000); /* Generate interrupt each 1 ms */
PORTC->PCR[5]= 256; //declared as GPIO
PTC->PDDR=0x00000010;
while(1){
PTC->PDOR=0x00000010;
delay(500);
PTC->PDOR=0x00000000;
delay(500);
}
void delay(unsigned int ms){
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<20000;j++);
}
}
Please help me in finding what is wrong with this code
Try this first see if the led comes on and stays on
# include "MK20D7.h"
int main(void)
{
PORTC->PCR[5]= 256; //declared as GPIO
PTC->PDDR=0x00000010;
PTC->PDOR=0x00000010;
//PTC->PDOR=0x00000000;
while(1) continue;
}
If not then you need to disassemble, see below.
Then try this
# include "MK20D7.h"
int main(void)
{
PORTC->PCR[5]= 256; //declared as GPIO
PTC->PDDR=0x00000010;
//PTC->PDOR=0x00000010;
PTC->PDOR=0x00000000;
while(1) continue;
}
and see if it goes/stays off
Then try this
# include "MK20D7.h"
void delay(unsigned int );
int main(void)
{
PORTC->PCR[5]= 256; //declared as GPIO
PTC->PDDR=0x00000010;
while(1)
{
PTC->PDOR=0x00000010;
delay();
PTC->PDOR=0x00000000;
delay();
}
}
void delay(void)
{
volatile unsigned int j;
for(j=0;j<20000;j++);
}
Assuming you have the right header file and the binary is built and linked properly, etc. Ideally you want to disassemble the binary, examine vector table to insure its placement and proper contents. Examine the code after the main label and compare it to the documentation for the few registers used.
PSOR and PCOR are easier to use
PTC->PSOR=1<<5;
delay();
PTC->PCOR=1<<5;
delay();
Related
I'm trying to communicate with the STM32 microcontroller via serial communication. Before I was using the USB cable and the communication worked flawlessly. However when I switched the same serial mode from USB to BLE (HM-10), the microcontroller fails to respond correctly for the first try, but it is okay after that. What could be a problem?
I tried searching if it was something to do with buffers or start/stop bits. But it didn't help me much because I had no clue about how to implement them through code. I'm using the MbedOS to program my dev board.
CODE:
#include "mbed.h"
DigitalOut led1(LED1);
#define MAX_INPUT_LENGTH 2
int code[MAX_INPUT_LENGTH];
Serial bt(PD_5, PD_6);
//Serial pc(USBTX, USBRX);
int main()
{
while(1)
{
while(bt.readable()==1)
{
volatile char str[2];
bt.scanf("%2s",str);
int index = 0;
while(index<=MAX_INPUT_LENGTH)
{
code[index] = str[index] - '0'; // convert to an int.
index++; // increase the index.
}
if(code[0]==0 && code[1]==1)
{
bt.printf("\n01 RECIEVED.");
for(int x=0;x<15;x++)
{
led1 = 1;
wait(0.25);
led1 = 0;
wait(0.25);
}
}
else
bt.printf("\nINCORRECT VALUE RECIEVED.");
}
}
}
I expected the output from ble serial to work the same way as the usb serial, but it failed for the first run.
Please bear with me. I am an amateur programmer at Arduino, and have never used Particle photon before.
I used an Arduino Uno and wrote the attached code that detects heart beat and temperature using the Grove Ear clip heart beat sensor and the Grove Temperature sensor and then prints them in the console every 20 seconds. Previously, this code was written to show in an Grove OLED screen but later simplified it back to using it without the OLED screen.
I am now looking into using the same application and function using the same sensor on a Particle Photon (as it is smaller and has WiFi capability). I have never previously used this technology before but I saw online that it, more or less, uses the same code application. I have been through the sample codes for this online available on its website but I have no idea how to convert my code for the Arduino into code for the photon (Photon console compiles the code without any error but does not show any sensor data). Can someone either point me to the right direction/ appropriate online resources / help me change this code here to make it work on the Photon? (I just added the Particle.publish() wherever I was using Arduino's Serial.println() but it still doesn't print anything).
The result in the console shows:
Please be ready
This will now begin
then it prints number 1 to 20 every second followed by sensor readings.
Again sorry for the inconvenience and thank you for your help.
I used direct codes from the documentation blog of the sensors (bottom of the page of the linked page):
Grove heart bear sensor
Grove Temperature sensor
#define LED 4//indicator, Grove - LED is connected with D4 of Arduino
boolean led_state = LOW;//state of LED, each time an external interrupt
//will change the state of LED
float tempa;
int tempPin = 0;
unsigned char counter;
unsigned long temp[21];
unsigned long sub;
bool data_effect=true;
unsigned int heart_rate;//the measurement result of heart rate
const int max_heartpluse_duty = 2000;//you can change it follow your system's request.
//2000 meams 2 seconds. System return error
//if the duty overtrip 2 second.
void setup()
{
pinMode(LED, OUTPUT);
Serial.begin(9600);
while (!Serial){
;
}
Serial.println("Please be ready");
delay(5000);
arrayInit();
Serial.println("This will now begin.");
attachInterrupt(0, interrupt, RISING);//set interrupt 0,digital port 2
}
void loop()
{
digitalWrite(LED, led_state);//Update the state of the indicator
}
/*Function: calculate the heart rate*/
void sum()
{
if(data_effect)
{
heart_rate=1200000/(temp[20]-temp[0]);//60*20*1000/20_total_time
Serial.print("Heart_rate_is:\t");
Serial.println(heart_rate);
tempa = analogRead(tempPin);
tempa = tempa * 0.11;
Serial.print("Body Temperature = ");
Serial.print(tempa);
Serial.print("*C");
Serial.println();
delay(1000);
}
data_effect=1;//sign bit
}
/*Function: Interrupt service routine.Get the sigal from the external interrupt*/
void interrupt()
{
temp[counter]=millis();
Serial.println(counter,DEC);
switch(counter)
{
case 0:
sub=temp[counter]-temp[20];
break;
default:
sub=temp[counter]-temp[counter-1];
break;
}
if(sub>max_heartpluse_duty)//set 2 seconds as max heart pluse duty
{
data_effect=0;//sign bit
counter=0;
Serial.println("measurement error,test will restart!" );
arrayInit();
}
else if (counter==20&&data_effect)
{
counter=0;
sum();
}
else if(counter!=20&&data_effect)
{
counter++;
}
else
{
counter=0;
data_effect=1;
}
}
/*Function: Initialization for the array(temp)*/
void arrayInit()
{
for(unsigned char i=0;i < 20;i ++)
{
temp[i]=0;
}
temp[20]=millis();
}
I am looking to communicate boolean values to and from two programs on arduino and windows console. I am working in C++ and using Visual Studio on the computer end and the standard arduino version of C++ on the arduino. All searches I have tried led me to using a windows form and a button on UI. I want this to all be done automatically and have both programs wait until the other has iterated. I've tried some Serial Port samples but none of them have worked so far for one reason or another.
This is the console code I have:
#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>
#include <iostream>
using namespace std;
const int PIXEL_X = 1153;
const int PIXEL_Y = 636;
const int RED = 0;
const int BLUE = 50;
const int GREEN = 200;
const int TOLERANCE = 30;
int main(void) {
COLORREF color;
HDC hDC;
do {// Get the device context for the screen
Sleep(50);
hDC = GetDC(NULL);
// Retrieve the color at that position
color = GetPixel(hDC, PIXEL_X, PIXEL_Y);
// Release the device context again
ReleaseDC(GetDesktopWindow(), hDC);
printf("%i %i %i", GetRValue(color), GetGValue(color), GetBValue(color));
int flag = 0;
if (fabs(GetRValue(color) - RED) > TOLERANCE) {
flag += 1;
}
if (fabs(GetGValue(color) - GREEN) > TOLERANCE) {
flag += 1;
}
if (fabs(GetBValue(color) - BLUE) > TOLERANCE) {
flag += 1;
}
if (flag >= 2) {
return 0;
}
//TODO 1: Send Serial data to continue and wait for return statement
} while (true);
return 0;
}
EDIT: This is my arduino code
#include <Servo.h>
Servo myServo;
Servo myServo2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myServo.attach(2);
myServo2.attach(3);
}
void A_button() {
myServo.write(125);
delay(1000);
myServo.write(97);
delay(250);
}
void L_button() {
myServo2.write(72);
delay(1000);
myServo2.write(90);
delay(250);
}
void loop() {
for(int i=0; i < 16; i++) {
A_button();
Serial.print("1 ");
delay(1500);
}
delay(3000);
// TODO 2: send Serial data
L_button();
Serial.print("0 ");
}
EDIT: Thank you for the response. I want the console application code to wait until the arduino code goes to TODO 2, then the console application runs until TODO 1 and checks if the RGB values are in their tolerances. If the RGB values are within the tolrance, then I want the arduino code to continue iterating until the TODO 2 again and repeat. I don't have any C++ code for the serial communication because none of the examples I've tried have compiled, and I can't use a windows form as I understand it.
I want to have the code wait at the TODO, but I have absolutely no idea how to do this and would appreciate any help. I will elaborate if any needs me to.
Thank you in advance.
I have tried to control motor stepper via arduino uno board with slider in visual C++.
but the servo didnot move at all.
here the program at PC side:
void CENVSConfigDlg::OnBnClickedButton1()
{
SetTimer(cTimer1,80,NULL);
}
void CENVSConfigDlg::OnTimer(UINT_PTR ID)
{
if(ID==cTimer1){
DWORD nbytes;
char buf[5];
sprintf(buf, "%d \n", val_test);
/* Open serial port. */
if(!WriteFile( hnd_serial, (void*)buf, 5, &nbytes, NULL )){MessageBox(L"Write Com Port fail!");return;}
}
and the program in arduino:
#include <Servo.h>
Servo servoMain;
int index=0;
String inputString;
void setup()
{
Serial.begin(9600);
servoMain.attach(9);
}
void loop()
{
int data;
while (Serial.available())
{
char inChar = (char)Serial.read();
if (inChar == '\n' || inChar == 'z')
{
data=stringToInt(inputString);
Serial.println(data); //
inputString="";
servoMain.write(data); //tambahannya
delay(50);
break;
}
if (inChar != 0)
{
inputString += inChar;
}
}
}
int stringToInt(String s)
{
char char_string[s.length()+1];
s.toCharArray(char_string, s.length()+1);
return atoi(char_string);
}
the pc i think is sending the data, but why the motor didnot working? any idea?
First off, does the serial link work at all? The number of ways that an RS232 link can not work, for both hardware and software reasons, is legendary. Unless you can show that the hardware can transfer data, it's pointless to look at your dedicated software. If you have a scope, use that to see if anything is being transmitted and then check that it arrives at the correct pin on the arduino input. If you have no access to a scope, a small LED and a 4.7K resistor can be used to indicate that there is data on the line - it will flicker with the data.
I've run the simple serial program on my Arduino Uno, which just echos whatever you type to it. This works perfectly when run in the Arduino Sketch IDE (v22).
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(115200); // opens serial port, sets data rate
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
(Code taken from http://arduino.cc/en/Serial/Available)
However, I prefer not to use the Arduino IDE and would rather compile my C++ code with avr-g++, so I wrote this, which should function exactly the same as above:
extern "C" {
#include <avr/io.h>
}
#include <HardwareSerial.h>
extern "C" void __cxa_pure_virtual() { while(1); }
int main (void)
{
int incomingByte = 0;
Serial.begin(115200);
while (1) {
if (Serial.available() > 0) {
incomingByte = Serial.read();
//print as an ASCII character
Serial.print("received: ");
Serial.println(incomingByte, DEC);
}
}
return 1;
}
I compile and run it, but it doesn't work. I never see my text echoed back to me. I tried printing out the value of Serial.available() in the while(1) loop, and it's always zero. Whenever I type on the keyboard, I see the RX LED light up, but nothing happens after that. I can edit my code to successfully call Serial.println() as long as it's outside the Serial.available() conditional.
I've confirmed that my baud rate in my serial software is also set to 115200. And yes, my serial software is pointing to the right serial port.
What am I missing?
Arduino's original glue code looks like this:
#include <WProgram.h>
int main(void)
{
init();
setup();
for (;;)
loop();
return 0;
}
The init() stuff is missing in your code. init() is defined in $ARDUINO_HOME/hardware/arduino/cores/arduino/wiring.c, you can either link against it directly or just copy the code of init() into your code.
You probably have not properly initialized the UART port on the chip. This has to be done manually for microcontrollers, and the Arduino IDE was probably doing it for you. Check the AVR datasheet for your chip, specifically the serial port section.
Found the answer to my own question:
It turns out the HardwareSerial.h library relies on interrupts. This is something that is automagically taken care of for you when building with the Arduino IDE. If you aren't using the Arduino IDE (like me), then you must remember to enable interrupts on your own.
Just #include <avr/interrupt.h>, and call sei(); to turn on interrupts before you try to use the Serial Library.
cheers!