Skip to content

Commit

Permalink
Add sysclock and adc demo
Browse files Browse the repository at this point in the history
1.Fix adc cannot read full scale detect
2.ADC default 10bit mode
3.Renew examples code
  • Loading branch information
nulljun committed Jul 3, 2021
1 parent bcbcf4d commit 78371c9
Show file tree
Hide file tree
Showing 34 changed files with 200 additions and 90 deletions.
30 changes: 27 additions & 3 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,37 @@

安装包

1、点击文件,再点击首选项,得到如下界面
1、文件->首选项,得到如下界面

2、在附加开发板管理器网址输入如下网址:
https://raw.githubusercontent.com/nulllaborg/arduino_nulllab/master/package_nulllab_boards_index.json
![](./doc/pic/option_zh.png)

3、工具->开发板->开发板管理
搜索 nulllab
可以看到如下 选择最新版本安装
搜索nulllab,选择最新版本安装

## 示例程序

- [SysClock](https://github.com/nulllaborg/arduino_nulllab/blob/master/libraries/BasicsExamples/examples/SysClock/SysClock.ino) - 外部晶振和系统时钟配置方法
- [AnalogRead](https://github.com/nulllaborg/arduino_nulllab/blob/master/libraries/BasicsExamples/examples/AnalogRead/AnalogRead.ino) - 12位ADC读取方法

### SysClock 配置

- **sysClock()**
设置时钟晶振内部32K,32M,外部32K,400K,1M,2M,4M,6M,8M,12M,16M,32MHz
- **sysClockPrescale()**
设置系统时钟分频系数0,2,4,8,26,42,64,128分频

### AnalogRead 读取

- **analogReference()**
设置adc参考电压
DEFAULT 供电电压AVCC
EXTERNAL 外部参考电压IOREF
INTERNAL1V024 内部高精度1.024V
INTERNAL2V048 内部高精度2.048V
INTERNAL4V096 内部高精度4.096V
- **analogReadResolution()**
设置adc精度有两种ADC10BIT和ADC12BIT默认精度是10bit
- **sysClockOutput()**
1:使能D8引脚输出CPU系统主频 0:关闭D8输出
5 changes: 4 additions & 1 deletion cores/lgt8f/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ void digitalWrite(uint8_t, uint8_t);
int digitalRead(uint8_t);
void digitalToggle(uint8_t);
int analogRead(uint8_t);
#define ADC10BIT 10
#define ADC12BIT 12
void analogReference(uint8_t mode);
void analogWrite(uint8_t, uint16_t);
void analogReadResolution(uint8_t);
Expand Down Expand Up @@ -338,7 +340,8 @@ void pwmMode(uint8_t pin, uint8_t wmode, uint8_t fmode = PWM_FREQ_FAST, uint8_t
#define SYSCLK_DIV_128 0x7

void sysClock(uint8_t mode);

void sysClockPrescale(uint8_t div);
void sysClockOutput(uint8_t enable);
// Log(HSP v3.7):
// - for system tick based on timer 2
#if defined(TIMSK) && defined(TOIE2)
Expand Down
23 changes: 21 additions & 2 deletions cores/lgt8f/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

// Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (* /*func*/ )()) { return 0; }

uint8_t clock_set = 0;
// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
Expand Down Expand Up @@ -139,6 +139,23 @@ void sysClock(uint8_t mode)
PMCR = 0xb6;
OSC_DELAY();
}
clock_set = 1;
}

void sysClockPrescale(uint8_t divn)
{
GPIOR0 = 0x80 | (divn & 0xf);
CLKPR = 0x80;
CLKPR = GPIOR0;
}

void sysClockOutput(uint8_t enable)
{
if (enable)
CLKPR |= 0x20; // output cup fre to PB0
else
CLKPR &= ~(0x20);
//CLKPR |= 0x40; // output cup fre to PE5
}

#if defined(__LGT8FX8P__) || defined(__LGT8FX8E__)
Expand Down Expand Up @@ -492,10 +509,12 @@ int main(void)
{

#if defined(__LGT8F__)

lgt8fx8x_init();

#if defined(CLOCK_SOURCE)
lgt8fx8x_clk_src();
if(clock_set == 0)
lgt8fx8x_clk_src();
#endif

#endif
Expand Down
29 changes: 10 additions & 19 deletions cores/lgt8f/wiring_analog.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,14 @@
#include "wiring_private.h"
#include "pins_arduino.h"

uint8_t analog_resbit = 0;
uint8_t analog_resdir = 0;
uint8_t analog_resbit = ADC10BIT;
//uint8_t analog_resdir = 0;
uint8_t analog_reference = DEFAULT;

#if defined(__LGT8F__)
void analogReadResolution(uint8_t res)
{
if(res > 16) res = 16;

if(res > 12) {
analog_resbit = res - 12;
analog_resdir = 0;
} else {
analog_resbit = 12 - res;
analog_resdir = 1;
}
analog_resbit = res;
}
#endif

Expand All @@ -60,6 +52,7 @@ void analogReference(uint8_t mode)
} else {
VCAL = VCAL1;
}
dafa
#else
// set analog reference for ADC/DAC
if(analog_reference == EXTERNAL) {
Expand All @@ -79,7 +72,7 @@ void analogReference(uint8_t mode)
} else if(analog_reference == INTERNAL4V096) {
sbi(ADCSRD, REFS2);
VCAL = VCAL3; // 4.096V
} else {
} else {
VCAL = VCAL1; // 1.024V
}
}
Expand Down Expand Up @@ -166,11 +159,13 @@ int __analogRead(uint8_t pin)
pVal = 0;
#endif

#if 0
// gain-error correction
#if defined(__LGT8FX8E__)
pVal -= (pVal >> 5);
#elif defined(__LGT8FX8P__)
pVal -= (pVal >> 7);
#endif
#endif
// standard device from atmel
return pVal;
Expand All @@ -181,14 +176,10 @@ int analogRead(uint8_t pin)
uint16_t adcVal = __analogRead(pin);

#if defined(__LGT8F__)
if(analog_resbit == 0)
if (analog_resbit == ADC12BIT)
return adcVal;

if(analog_resdir == 1) {
return adcVal >> analog_resbit;
} else {
return adcVal << analog_resbit;
}
else
return adcVal&0x3FF;
#else
return adcVal;
#endif
Expand Down
Binary file added doc/pic/option_zh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
26 changes: 26 additions & 0 deletions libraries/BasicsExamples/examples/AnalogRead/AnalogRead.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//============================================
// ADC demo for lgt328p
// Nulllab org
// Using new added internal 2.56V reference
// analogReadResolution:
// ADC10BIT ADC12BIT

// analogReference :
// DEFAULT EXTERNAL INTERNAL1V024 INTERNAL2V048 INTERNAL4V096
//============================================

uint16_t value;

void setup() {
// put your setup code here, to run once:
analogReadResolution(ADC12BIT); // ADC10BIT default is 10bit
analogReference(INTERNAL2V48); // DEFAULT EXTERNAL INTERNAL1V024 INTERNAL2V048 INTERNAL4V096
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
value = analogRead(A0);
Serial.println(value);
delay(1);
}
31 changes: 31 additions & 0 deletions libraries/BasicsExamples/examples/SysClock/SysClock.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// demo for system clock switch
// using function "sysClock(mode)" to switch
// system clock between internal or external oscillator
// e.g:
// INT_OSC_32K 0
// INT_OSC_32M 1
// EXT_OSC_32M 2
// EXT_OSC_24M 3
// EXT_OSC_16M 4
// EXT_OSC_12M 5
// EXT_OSC_8M 6
// EXT_OSC_4M 7
// EXT_OSC_2M 8
// EXT_OSC_1M 9
// EXT_OSC_400K 10
// EXT_OSC_32K 11
// sysClock(INT_OSC_32M) for internal 32M oscillator
// sysClock(EXT_OSC_16M) for external 16M crystal

void setup() {
// put your setup code here, to run once:
sysClock(INT_OSC_32M); // Set internal 32M oscillator
sysClockPrescale(SYSCLK_DIV_2); // set clock prescale 0 2 4 8 16 32 64 128
sysClockOutput(1); // sysclock out put to D8
pinMode(13, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalToggle(13);
}
File renamed without changes.
9 changes: 9 additions & 0 deletions libraries/BasicsExamples/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=BasicsExamples
version=0.0.0
author=LGTMCU
maintainer=LGTMCU
sentence=Example sketches for the Nulllab Arduino Hardware Support Package
paragraph=
category=Other
url=https://github.com/nulllaborg/arduino_nulllab
architectures=avr

This file was deleted.

This file was deleted.

9 changes: 0 additions & 9 deletions libraries/Larduino_HSPExamples/library.properties

This file was deleted.

52 changes: 41 additions & 11 deletions libraries/Syntax/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
#######################################
# Methods and Functions (KEYWORD2)
#######################################
pwmMode KEYWORD2
pwmWrite KEYWORD2
pwmFrequency KEYWORD2
pwmTurnOff KEYWORD2
startTick KEYWORD2
stopTick KEYWORD2
sysClock KEYWORD2
digitalToggle KEYWORD2
pwmResolution KEYWORD2
pwmMode KEYWORD2
pwmWrite KEYWORD2
pwmFrequency KEYWORD2
pwmTurnOff KEYWORD2
startTick KEYWORD2
stopTick KEYWORD2
sysClock KEYWORD2
sysClockPrescale KEYWORD2
sysClockOutput KEYWORD2
digitalToggle KEYWORD2
pwmResolution KEYWORD2
fastioMode KEYWORD2
fastioToggle KEYWORD2
fastioRead KEYWORD2
Expand All @@ -42,12 +44,40 @@ PWM_FREQ_BOOST LITERAL1
PWM_FREQ_FAST LITERAL1
PWM_FREQ_NORMAL LITERAL1
PWM_FREQ_SLOW LITERAL1
INT_OSC LITERAL1
EXT_OSC LITERAL1

INT_OSC_32K LITERAL1
INT_OSC_32M LITERAL1
EXT_OSC_32M LITERAL1
EXT_OSC_24M LITERAL1
EXT_OSC_16M LITERAL1
EXT_OSC_12M LITERAL1
EXT_OSC_8M LITERAL1
EXT_OSC_4M LITERAL1
EXT_OSC_2M LITERAL1
EXT_OSC_1M LITERAL1
EXT_OSC_400K LITERAL1
EXT_OSC_32K LITERAL1

SYSCLK_DIV_0 LITERAL1
SYSCLK_DIV_2 LITERAL1
SYSCLK_DIV_4 LITERAL1
SYSCLK_DIV_8 LITERAL1
SYSCLK_DIV_16 LITERAL1
SYSCLK_DIV_32 LITERAL1
SYSCLK_DIV_64 LITERAL1
SYSCLK_DIV_128 LITERAL1

OC3A LITERAL1
VCCM LITERAL1
AOFF LITERAL1
IVREF LITERAL1
ADC10BIT LITERAL1
ADC12BIT LITERAL1
DEFAULT LITERAL1
EXTERNAL LITERAL1
INTERNAL1V024 LITERAL1
INTERNAL2V048 LITERAL1
INTERNAL4V096 LITERAL1
AGND LITERAL1
V5D1 LITERAL1
V5D4 LITERAL1
Expand Down
Loading

0 comments on commit 78371c9

Please sign in to comment.