Skip to content

Commit

Permalink
Add gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
atick-faisal committed Oct 18, 2023
1 parent 609bfcd commit 99af291
Show file tree
Hide file tree
Showing 86 changed files with 4,833 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#Ignore List for Microchip's MPLAB X IDE
#It's a form of NetBeans with vendor specific changes
#Taken from zeha on GIST https://gist.github.com/zeha/5999375
#Updated by Cristian Cristea (https://github.com/cristiancristea00)

*.d
*.pre
*.p1
*.lst
*.sym
*.obj
*.o
*.sdb
*.obj.dmp
*.mk
*.map
*.properties
*.production
*.debug
html/
nbproject/private/
nbproject/Package-*.bash
build/
nbbuild/
dist/
nbdist/
nbactions.xml
nb-configuration.xml
funclist
nbproject/Makefile-*
disassembly/
.generated_files/
113 changes: 113 additions & 0 deletions Analog to Digital Conversion/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#
# There exist several targets which are by default empty and which can be
# used for execution of your targets. These targets are usually executed
# before and after some main targets. They are:
#
# .build-pre: called before 'build' target
# .build-post: called after 'build' target
# .clean-pre: called before 'clean' target
# .clean-post: called after 'clean' target
# .clobber-pre: called before 'clobber' target
# .clobber-post: called after 'clobber' target
# .all-pre: called before 'all' target
# .all-post: called after 'all' target
# .help-pre: called before 'help' target
# .help-post: called after 'help' target
#
# Targets beginning with '.' are not intended to be called on their own.
#
# Main targets can be executed directly, and they are:
#
# build build a specific configuration
# clean remove built files from a configuration
# clobber remove all built files
# all build all configurations
# help print help mesage
#
# Targets .build-impl, .clean-impl, .clobber-impl, .all-impl, and
# .help-impl are implemented in nbproject/makefile-impl.mk.
#
# Available make variables:
#
# CND_BASEDIR base directory for relative paths
# CND_DISTDIR default top distribution directory (build artifacts)
# CND_BUILDDIR default top build directory (object files, ...)
# CONF name of current configuration
# CND_ARTIFACT_DIR_${CONF} directory of build artifact (current configuration)
# CND_ARTIFACT_NAME_${CONF} name of build artifact (current configuration)
# CND_ARTIFACT_PATH_${CONF} path to build artifact (current configuration)
# CND_PACKAGE_DIR_${CONF} directory of package (current configuration)
# CND_PACKAGE_NAME_${CONF} name of package (current configuration)
# CND_PACKAGE_PATH_${CONF} path to package (current configuration)
#
# NOCDDL


# Environment
MKDIR=mkdir
CP=cp
CCADMIN=CCadmin
RANLIB=ranlib


# build
build: .build-post

.build-pre:
# Add your pre 'build' code here...

.build-post: .build-impl
# Add your post 'build' code here...


# clean
clean: .clean-post

.clean-pre:
# Add your pre 'clean' code here...
# WARNING: the IDE does not call this target since it takes a long time to
# simply run make. Instead, the IDE removes the configuration directories
# under build and dist directly without calling make.
# This target is left here so people can do a clean when running a clean
# outside the IDE.

.clean-post: .clean-impl
# Add your post 'clean' code here...


# clobber
clobber: .clobber-post

.clobber-pre:
# Add your pre 'clobber' code here...

.clobber-post: .clobber-impl
# Add your post 'clobber' code here...


# all
all: .all-post

.all-pre:
# Add your pre 'all' code here...

.all-post: .all-impl
# Add your post 'all' code here...


# help
help: .help-post

.help-pre:
# Add your pre 'help' code here...

.help-post: .help-impl
# Add your post 'help' code here...



# include project implementation makefile
include nbproject/Makefile-impl.mk

# include project make variables
include nbproject/Makefile-variables.mk
Binary file not shown.
52 changes: 52 additions & 0 deletions Analog to Digital Conversion/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## ADC - Analog to Digital Conversion

<p align="center">
<img src="https://cdn.openlabpro.com/wp-content/uploads/2016/09/ADC-1.png" width="400"/>
</p>

The Analog-to-Digital (A/D) Converter module has five inputs for the 28-pin devices and eight for the 40/44-pin devices. The conversion of an analog input signal results in a corresponding `10-bit` digital number. The A/D module has high and low-voltage `reference input` that is software selectable to some combination of VDD, VSS, RA2 or RA3. The A/D converter has a unique feature of being able to operate while the device is in Sleep mode. To operate in Sleep, the A/D clock must be derived from the A/D’s internal RC oscillator.

**The A/D module has four registers. These registers are:**
- A/D Result High Register `(ADRESH)`
- A/D Result Low Register `(ADRESL)`
- A/D Control Register 0 `(ADCON0)`
- A/D Control Register 1 `(ADCON1)`

**Steps in A/D Conversion**
- Configure `ADCON0` register to set the clock frequency, set up channels and powering up the A/D module.
```c
ADCON0bits.ADCS = 0b00; // set A/D conversion clock = fosc/2
ADCON0bits.CHS = 0b000; // set all adc channels off
ADCON0bits.ADON = 0; // a/d module is powered off
```
- Configure `ADCON1` to set the justification of 10bit output (Ouptut is stored in both `ADRESH` and `ADRESL`), clock frequency and data direction of analog input pins.
```c
ADCON1bits.ADFM = 1; // set right justification for ADRESH
ADCON1bits.ADCS2 = 0; // set A/D conversion clock = fosc/2
ADCON1bits.PCFG = 0b0000; // set all pins as adc input
```
- Read analog input value
```c
int __adc_read(int adc_channel) {
ADCON0bits.ADON = 1; // turn on a/d module
ADCON0bits.CHS = adc_channel; // turn on adc channel
__delay_ms(10); // wait for capacitors to charge up
ADCON0bits.GO = 1; // begin conversion
while(ADCON0bits.GO_DONE == 1) {
// wait for conversion to finish
}
int adc_value = (ADRESH << 8) + (ADRESL);
return adc_value;
}
```
### Circuit Diagram
<p align="center">
<img src="adc.PNG" width="600"/>
</p>
## License
[![licensebuttons by-nc-sa](https://licensebuttons.net/l/by-nc-sa/3.0/88x31.png)](https://creativecommons.org/licenses/by-nc-sa/4.0)
This work is licensed under [GNU General Public License v3.0](https://github.com/atick-faisal/PIC16F877a/blob/master/LICENSE).
Binary file added Analog to Digital Conversion/adc.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
103 changes: 103 additions & 0 deletions Analog to Digital Conversion/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Code: Analog to Digital Conversion
* Author: Atick Faisal
* License: GPL-3.0
* Created on March 18, 2019, 2:29 AM
*/


#include "pic16f877a.h"

#define _XTAL_FREQ 16000000
#define PRESCALAR 16
#define ADC_PIN 1

float PWM_FREQ;
float PWM_PERIOD;
int counter;

void __init_adc() {
/*
* ADCON0 Register
* +---------------------------------------------------------------+
* | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
* +---------------------------------------------------------------+
* | ADCS | CSH |GO/DONE| - | ADON |
* +---------------------------------------------------------------+
*/

ADCON0bits.ADCS = 0b00; // set A/D conversion clock = fosc/2
ADCON0bits.CHS = 0b000; // set all adc channels off
ADCON0bits.ADON = 0; // a/d module is powered off

/*
* ADCON1 Register
* +---------------------------------------------------------------+
* | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
* +---------------------------------------------------------------+
* | ADFM | ADCS2 | - | - | PCFG |
* +---------------------------------------------------------------+
*/

ADCON1bits.ADFM = 1; // set right justification for ADRESH
ADCON1bits.ADCS2 = 0; // set A/D conversion clock = fosc/2
ADCON1bits.PCFG = 0b0000; // set all pins as adc input
}

int __adc_read(int adc_channel) {
ADCON0bits.ADON = 1; // turn on a/d module
ADCON0bits.CHS = (unsigned char) adc_channel; // turn on adc channel
__delay_ms(10); // wait for capacitors to charge up
ADCON0bits.GO = 1; // begin conversion
while (ADCON0bits.GO_DONE == 1) {
// wait for conversion to finish
}
int adc_value = (ADRESH << 8) + (ADRESL);
return adc_value;
}

///////////////////////////// PWM Configuration ////////////////////////////////

void __set_pwm_freq(int f) {
PWM_FREQ = f;
PWM_PERIOD = 1 / PWM_FREQ;
int PR2_value = (int) ((PWM_PERIOD * _XTAL_FREQ) / (4 * PRESCALAR) - 1);
PR2 = (uint8_t) PR2_value;

}

void __init_pwm() {
TRISCbits.TRISC2 = 0; // set RC2 as output
T2CONbits.TMR2ON = 1; // set timer 2 on
T2CONbits.T2CKPS = 0b10; // set pre scalar of 16
CCP1CONbits.CCP1M = 0b1100; // set PWM mode of operation
}

void __set_duty_cycle(int duty_cycle) {
float dc = (float) duty_cycle / 100;
float dc_period = dc * PWM_PERIOD;
uint8_t reg_value = (uint8_t) ((dc_period * _XTAL_FREQ) / PRESCALAR);
/*
* PWM has 10 bit resolution
* 8 bits of MSB is stored in CCPR1L
* 2 bits of LSB is stored in CCP1CON(5:4)
*/
CCPR1L = reg_value >> 2;
CCP1CONbits.CCP1X = (reg_value & 0b00000001);
CCP1CONbits.CCP1Y = (reg_value & 0b00000010);
}
////////////////////////////////////////////////////////////////////////////////

void main(void) {
__set_pwm_freq(2000);
__init_pwm();
__set_duty_cycle(0);

__init_adc();

while (1) {
float adc_value = (float) __adc_read(ADC_PIN);
int pwm_value = (int) (adc_value * 100) / 1023; // scale value between 0 to 100
__set_duty_cycle(pwm_value);
}
}
73 changes: 73 additions & 0 deletions Analog to Digital Conversion/nbproject/Package-default.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/bash -x

#
# Generated - do not edit!
#

# Macros
TOP=`pwd`
CND_CONF=default
CND_DISTDIR=dist
TMPDIR=build/${CND_CONF}/${IMAGE_TYPE}/tmp-packaging
TMPDIRNAME=tmp-packaging
OUTPUT_PATH=dist/${CND_CONF}/${IMAGE_TYPE}/ADC.X.${IMAGE_TYPE}.${OUTPUT_SUFFIX}
OUTPUT_BASENAME=ADC.X.${IMAGE_TYPE}.${OUTPUT_SUFFIX}
PACKAGE_TOP_DIR=adc.x/

# Functions
function checkReturnCode
{
rc=$?
if [ $rc != 0 ]
then
exit $rc
fi
}
function makeDirectory
# $1 directory path
# $2 permission (optional)
{
mkdir -p "$1"
checkReturnCode
if [ "$2" != "" ]
then
chmod $2 "$1"
checkReturnCode
fi
}
function copyFileToTmpDir
# $1 from-file path
# $2 to-file path
# $3 permission
{
cp "$1" "$2"
checkReturnCode
if [ "$3" != "" ]
then
chmod $3 "$2"
checkReturnCode
fi
}

# Setup
cd "${TOP}"
mkdir -p ${CND_DISTDIR}/${CND_CONF}/package
rm -rf ${TMPDIR}
mkdir -p ${TMPDIR}

# Copy files and create directories and links
cd "${TOP}"
makeDirectory ${TMPDIR}/adc.x/bin
copyFileToTmpDir "${OUTPUT_PATH}" "${TMPDIR}/${PACKAGE_TOP_DIR}bin/${OUTPUT_BASENAME}" 0755


# Generate tar file
cd "${TOP}"
rm -f ${CND_DISTDIR}/${CND_CONF}/package/adc.x.tar
cd ${TMPDIR}
tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/package/adc.x.tar *
checkReturnCode

# Cleanup
cd "${TOP}"
rm -rf ${TMPDIR}
Loading

0 comments on commit 99af291

Please sign in to comment.