- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Dear Arath-san and for those who may be interested,
I created a sample project using CY8CKIT-044.
Request
==========================
What will be the code for:
When the ADC measure 4V display the label "Maximum Voltage" and display it
in Putty terminal.
==========================
Note: As it must handle 4V (or higher), I set J9 of CY8CKIT-044 to 3-2 (5V)
Also in the Design Wide Resources > System
I set VDDA, VDDD, VDDIO to 5.0
schematic
Note: To test the program, I connected a POT (5.0V, P2(0), GND)
pins
ADC Configuration
main.c
===================
#include "project.h"
#include "stdio.h"
#define STR_BUF_LEN 64
char str[STR_BUF_LEN+1] ;
void print(char *str)
{
UART_UartPutString(str) ;
}
void cls(void)
{
print("\033c") ; /* reset */
CyDelay(20) ;
print("\033[2J") ; /* clear screen */
CyDelay(20) ;
}
void splash(char *prog_name)
{
cls() ;
if (prog_name && *prog_name) {
print(prog_name) ;
}
print(" (") ;
print(__DATE__) ;
print(" ") ;
print(__TIME__) ;
print(")\n\r") ;
}
void init_hardware(void)
{
CyGlobalIntEnable; /* Enable global interrupts. */
UART_Start() ;
ADC_Start() ;
}
int16_t measure(void)
{
int16_t raw_count ;
int16_t mV ;
int ch = 0 ;
ADC_StartConvert() ;
ADC_IsEndConversion(ADC_WAIT_FOR_RESULT) ;
raw_count = ADC_GetResult16(ch) ;
mV = ADC_CountsTo_mVolts(ch, raw_count) ;
return( mV ) ;
}
void show_value(int16_t mV)
{
int overflow_flag = 0 ;
int negative = 0 ;
if (mV >= 4000) {
overflow_flag = 1 ;
}
if (mV < 0) {
negative = 1 ;
mV = -1 * mV ;
print("-");
}
snprintf(str, STR_BUF_LEN, "%d.%03d ", mV/1000, mV%1000) ;
print(str) ;
if (overflow_flag) {
print("Maximum Voltage") ;
}
print("\n\r") ;
}
int main(void)
{
int16_t mV ;
init_hardware() ;
splash("CY8CKIT-044 ADC Test with limit Voltage") ;
for(;;)
{
mV = measure() ;
show_value(mV) ;
CyDelay(1000) ;
}
}
===================
putty setup
putty output
moto
P.S. BTW, you could post a question/request in this Code Example Space,
then you may not have to wait for a day. (As I was taking off, yesterday)
- Labels:
-
PSoC 345 LP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tanaka-san
I appreciate your help and fast response!
Muchas Gracias!
よろしくおねがいします
Arath
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
One last question Tanaka-san
Will be the same code for CY8CKIT-059?
Only want to re confirm
Thanks
Arath
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Dear Arath-san,
> Will be the same code for CY8CKIT-059?
LOL, NO.
Since you were referencing my Ryukoku Topic, I assumed that you were talking about PSoC 4.
Anyway, not a big difference(s), a version for CY8CKIT-059 here.
Schematic
Pins
main.c
===================
#include "project.h"
#include "stdio.h"
#define STR_BUF_LEN 64
char str[STR_BUF_LEN+1] ;
void print(char *str)
{
UART_PutString(str) ;
}
void cls(void)
{
print("\033c") ; /* reset */
CyDelay(20) ;
print("\033[2J") ; /* clear screen */
CyDelay(20) ;
}
void splash(char *prog_name)
{
cls() ;
if (prog_name && *prog_name) {
print(prog_name) ;
}
print(" (") ;
print(__DATE__) ;
print(" ") ;
print(__TIME__) ;
print(")\n\r") ;
}
void init_hardware(void)
{
CyGlobalIntEnable; /* Enable global interrupts. */
UART_Start() ;
ADC_Start() ;
}
int16_t measure(void)
{
int16_t raw_count ;
int16_t mV ;
ADC_StartConvert() ;
ADC_IsEndConversion(ADC_WAIT_FOR_RESULT) ;
raw_count = ADC_GetResult16() ;
mV = ADC_CountsTo_mVolts(raw_count) ;
return( mV ) ;
}
void show_value(int16_t mV)
{
int overflow_flag = 0 ;
int negative = 0 ;
if (mV >= 4000) {
overflow_flag = 1 ;
}
if (mV < 0) {
negative = 1 ;
mV = -1 * mV ;
print("-");
}
snprintf(str, STR_BUF_LEN, "%d.%03d ", mV/1000, mV%1000) ;
print(str) ;
if (overflow_flag) {
print("Maximum Voltage") ;
}
print("\n\r") ;
}
int main(void)
{
int16_t mV ;
init_hardware() ;
splash("CY8CKIT-059 ADC Test with limit Voltage") ;
for(;;)
{
mV = measure() ;
show_value(mV) ;
CyDelay(1000) ;
}
}
===================
Best Regards,
21-Oct-2020
Motoo Tanaka