uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

commit 7b8b10085a264305d59c051e4b4545fb0ac689fd
parent b34bb297544ccf88682cf951526e59ac7470e770
Author: Christos Margiolis <christos@margiolis.net>
Date:   Sun, 23 Jan 2022 03:45:17 +0200

set up tmr0 i think

Diffstat:
Mc_embedded/schem/pic.sch | 110+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
Mc_embedded/schem/pic.sch-bak | 434+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Mc_embedded/src/main.c | 135+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------
3 files changed, 410 insertions(+), 269 deletions(-)

diff --git a/c_embedded/schem/pic.sch b/c_embedded/schem/pic.sch @@ -14,17 +14,6 @@ Comment3 "" Comment4 "" $EndDescr $Comp -L Device:LED LED1 -U 1 1 61E8F822 -P 9350 5200 -F 0 "LED1" H 9343 5325 50 0000 C CNN -F 1 "LED" H 9343 5326 50 0001 C CNN -F 2 "" H 9350 5200 50 0001 C CNN -F 3 "~" H 9350 5200 50 0001 C CNN - 1 9350 5200 - 1 0 0 -1 -$EndComp -$Comp L Device:R_US R1 U 1 1 61EA1FDB P 5300 3000 @@ -203,19 +192,15 @@ Connection ~ 6550 2450 $Comp L Sensor:BME280 BME280 U 1 1 61E953D6 -P 8600 3250 -F 0 "BME280" H 8171 3296 50 0000 R CNN -F 1 "BME280" H 8171 3205 50 0001 R CNN -F 2 "Package_LGA:Bosch_LGA-8_2.5x2.5mm_P0.65mm_ClockwisePinNumbering" H 10100 2800 50 0001 C CNN -F 3 "https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME280-DS002.pdf" H 8600 3050 50 0001 C CNN - 1 8600 3250 +P 8750 2600 +F 0 "BME280" H 8321 2646 50 0000 R CNN +F 1 "BME280" H 8321 2555 50 0001 R CNN +F 2 "Package_LGA:Bosch_LGA-8_2.5x2.5mm_P0.65mm_ClockwisePinNumbering" H 10250 2150 50 0001 C CNN +F 3 "https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME280-DS002.pdf" H 8750 2400 50 0001 C CNN + 1 8750 2600 -1 0 0 -1 $EndComp Wire Wire Line - 8000 3150 7800 3150 -Wire Wire Line - 7800 3150 7800 4900 -Wire Wire Line 6650 2700 6650 2450 $Comp L Display_Character:NHD-0420H1Z LCD @@ -324,44 +309,79 @@ Connection ~ 6550 6150 Wire Wire Line 6550 6150 6550 6300 Wire Wire Line - 7800 4900 7450 4900 + 6000 1750 6000 1950 +Wire Wire Line + 6550 5700 6550 6150 Wire Wire Line - 7450 5000 7900 5000 + 6650 5600 6650 5700 Wire Wire Line - 7900 5000 7900 3350 + 6650 5700 6550 5700 Wire Wire Line - 7900 3350 8000 3350 + 8650 6150 6550 6150 Wire Wire Line - 8000 2950 7700 2950 + 8650 1950 6000 1950 Wire Wire Line - 7700 2950 7700 5100 + 7450 4900 7550 4900 Wire Wire Line - 7700 5100 7450 5100 + 7550 4900 7550 2500 Wire Wire Line - 6000 1750 6000 1950 + 7550 2500 8150 2500 Wire Wire Line - 6550 5700 6550 6150 + 7450 5000 7650 5000 Wire Wire Line - 6650 5600 6650 5700 + 7650 5000 7650 2700 Wire Wire Line - 6650 5700 6550 5700 + 7650 2700 8150 2700 +Wire Wire Line + 7450 5100 7750 5100 +Wire Wire Line + 7750 5100 7750 2300 +Wire Wire Line + 7750 2300 8150 2300 +Wire Wire Line + 8650 3200 8650 3700 +Connection ~ 8650 3700 +Wire Wire Line + 8450 3700 8650 3700 $Comp -L Switch:SW_Push BTN1 -U 1 1 620E2C06 -P 9800 5200 -F 0 "BTN1" H 9800 5393 50 0000 C CNN -F 1 "SW_Push" H 9800 5394 50 0001 C CNN -F 2 "" H 9800 5400 50 0001 C CNN -F 3 "~" H 9800 5400 50 0001 C CNN - 1 9800 5200 - 1 0 0 -1 +L Device:LED LED1 +U 1 1 61E8F822 +P 8300 3700 +F 0 "LED1" H 8293 3825 50 0000 C CNN +F 1 "LED" H 8293 3826 50 0001 C CNN +F 2 "" H 8300 3700 50 0001 C CNN +F 3 "~" H 8300 3700 50 0001 C CNN + 1 8300 3700 + -1 0 0 1 $EndComp Wire Wire Line - 8500 3850 8500 6150 + 8100 3700 8150 3700 Wire Wire Line - 8500 6150 6550 6150 + 7450 3700 7800 3700 +$Comp +L Device:R_US R3 +U 1 1 61F053CB +P 7950 3700 +F 0 "R3" H 8018 3746 50 0000 L CNN +F 1 "330Ω" H 8018 3655 50 0000 L CNN +F 2 "" V 7990 3690 50 0001 C CNN +F 3 "~" H 7950 3700 50 0001 C CNN + 1 7950 3700 + 0 -1 -1 0 +$EndComp Wire Wire Line - 8500 2650 8500 1950 + 8650 2000 8650 1950 Wire Wire Line - 8500 1950 6000 1950 + 8650 3700 8650 6150 +$Comp +L Switch:SW_Push BTN1 +U 1 1 620E2C06 +P 9250 4050 +F 0 "BTN1" H 9250 4243 50 0000 C CNN +F 1 "SW_Push" H 9250 4244 50 0001 C CNN +F 2 "" H 9250 4250 50 0001 C CNN +F 3 "~" H 9250 4250 50 0001 C CNN + 1 9250 4050 + 1 0 0 -1 +$EndComp $EndSCHEMATC diff --git a/c_embedded/schem/pic.sch-bak b/c_embedded/schem/pic.sch-bak @@ -14,354 +14,374 @@ Comment3 "" Comment4 "" $EndDescr $Comp -L Device:LED LED -U 1 1 61E8F822 -P 8100 4650 -F 0 "LED" H 8093 4775 50 0000 C CNN -F 1 "LED" H 8093 4776 50 0001 C CNN -F 2 "" H 8100 4650 50 0001 C CNN -F 3 "~" H 8100 4650 50 0001 C CNN - 1 8100 4650 - 1 0 0 -1 -$EndComp -$Comp L Device:R_US R1 U 1 1 61EA1FDB -P 4050 2450 -F 0 "R1" H 4118 2496 50 0000 L CNN -F 1 "10kΩ" H 4118 2405 50 0000 L CNN -F 2 "" V 4090 2440 50 0001 C CNN -F 3 "~" H 4050 2450 50 0001 C CNN - 1 4050 2450 +P 5300 3000 +F 0 "R1" H 5368 3046 50 0000 L CNN +F 1 "10kΩ" H 5368 2955 50 0000 L CNN +F 2 "" V 5340 2990 50 0001 C CNN +F 3 "~" H 5300 3000 50 0001 C CNN + 1 5300 3000 0 -1 -1 0 $EndComp $Comp L power:GNDREF GND U 1 1 61EB279A -P 4200 1600 -F 0 "GND" H 4288 1563 50 0000 L CNN -F 1 "GNDREF" H 4205 1427 50 0001 C CNN -F 2 "" H 4200 1600 50 0001 C CNN -F 3 "" H 4200 1600 50 0001 C CNN - 1 4200 1600 +P 5450 2150 +F 0 "GND" H 5538 2113 50 0000 L CNN +F 1 "GNDREF" H 5455 1977 50 0001 C CNN +F 2 "" H 5450 2150 50 0001 C CNN +F 3 "" H 5450 2150 50 0001 C CNN + 1 5450 2150 -1 0 0 1 $EndComp $Comp L Device:R_US R2 U 1 1 61ECA8DC -P 2850 5400 -F 0 "R2" H 2918 5446 50 0000 L CNN -F 1 "330Ω" H 2918 5355 50 0000 L CNN -F 2 "" V 2890 5390 50 0001 C CNN -F 3 "~" H 2850 5400 50 0001 C CNN - 1 2850 5400 +P 4100 5950 +F 0 "R2" H 4168 5996 50 0000 L CNN +F 1 "330Ω" H 4168 5905 50 0000 L CNN +F 2 "" V 4140 5940 50 0001 C CNN +F 3 "~" H 4100 5950 50 0001 C CNN + 1 4100 5950 1 0 0 -1 $EndComp $Comp L Device:C_Small C1 U 1 1 61E97CC0 -P 2850 3200 -F 0 "C1" H 2942 3246 50 0000 L CNN -F 1 "22pF" H 2942 3155 50 0000 L CNN -F 2 "" H 2850 3200 50 0001 C CNN -F 3 "~" H 2850 3200 50 0001 C CNN - 1 2850 3200 +P 4100 3750 +F 0 "C1" H 4192 3796 50 0000 L CNN +F 1 "22pF" H 4192 3705 50 0000 L CNN +F 2 "" H 4100 3750 50 0001 C CNN +F 3 "~" H 4100 3750 50 0001 C CNN + 1 4100 3750 -1 0 0 -1 $EndComp $Comp L power:GNDREF GND U 1 1 61EE8AF0 -P 3050 3350 -F 0 "GND" H 3138 3313 50 0000 L CNN -F 1 "GNDREF" H 3055 3177 50 0001 C CNN -F 2 "" H 3050 3350 50 0001 C CNN -F 3 "" H 3050 3350 50 0001 C CNN - 1 3050 3350 +P 4300 3900 +F 0 "GND" H 4388 3863 50 0000 L CNN +F 1 "GNDREF" H 4305 3727 50 0001 C CNN +F 2 "" H 4300 3900 50 0001 C CNN +F 3 "" H 4300 3900 50 0001 C CNN + 1 4300 3900 1 0 0 -1 $EndComp $Comp L Device:C_Small C2 U 1 1 61ECA409 -P 3250 3200 -F 0 "C2" H 3342 3246 50 0000 L CNN -F 1 "22pF" H 3342 3155 50 0000 L CNN -F 2 "" H 3250 3200 50 0001 C CNN -F 3 "~" H 3250 3200 50 0001 C CNN - 1 3250 3200 +P 4500 3750 +F 0 "C2" H 4592 3796 50 0000 L CNN +F 1 "22pF" H 4592 3705 50 0000 L CNN +F 2 "" H 4500 3750 50 0001 C CNN +F 3 "~" H 4500 3750 50 0001 C CNN + 1 4500 3750 1 0 0 -1 $EndComp $Comp -L Device:Crystal OSC +L Device:Crystal OSC1 U 1 1 61EB43E4 -P 3050 3000 -F 0 "OSC" H 3050 3268 50 0000 C CNN -F 1 "8MHz" H 3050 3177 50 0000 C CNN -F 2 "" H 3050 3000 50 0001 C CNN -F 3 "~" H 3050 3000 50 0001 C CNN - 1 3050 3000 +P 4300 3550 +F 0 "OSC1" H 4300 3818 50 0000 C CNN +F 1 "8MHz" H 4300 3727 50 0000 C CNN +F 2 "" H 4300 3550 50 0001 C CNN +F 3 "~" H 4300 3550 50 0001 C CNN + 1 4300 3550 1 0 0 -1 $EndComp $Comp -L MCU_Microchip_PIC16:PIC16F877A-IP PIC16F877A-IP +L MCU_Microchip_PIC16:PIC16F877A-IP PIC16F877A-IP1 U 1 1 61E8B28E -P 5300 3550 -F 0 "PIC16F877A-IP" H 5300 5039 50 0000 C CNN -F 1 "PIC16F877A-IP" H 5300 5040 50 0001 C CNN -F 2 "" H 5300 3550 50 0001 C CIN -F 3 "http://ww1.microchip.com/downloads/en/DeviceDoc/39582b.pdf" H 5300 3550 50 0001 C CNN - 1 5300 3550 +P 6550 4100 +F 0 "PIC16F877A-IP1" H 6550 5589 50 0000 C CNN +F 1 "PIC16F877A-IP" H 6550 5590 50 0001 C CNN +F 2 "" H 6550 4100 50 0001 C CIN +F 3 "http://ww1.microchip.com/downloads/en/DeviceDoc/39582b.pdf" H 6550 4100 50 0001 C CNN + 1 6550 4100 1 0 0 -1 $EndComp Wire Wire Line - 2850 2750 2850 3000 + 4100 3300 4100 3550 Wire Wire Line - 2900 3000 2850 3000 -Connection ~ 2850 3000 + 4150 3550 4100 3550 +Connection ~ 4100 3550 Wire Wire Line - 2850 3000 2850 3100 + 4100 3550 4100 3650 Wire Wire Line - 3250 2950 3250 3000 + 4500 3500 4500 3550 Wire Wire Line - 3200 3000 3250 3000 -Connection ~ 3250 3000 + 4450 3550 4500 3550 +Connection ~ 4500 3550 Wire Wire Line - 3250 3000 3250 3100 + 4500 3550 4500 3650 Wire Wire Line - 3050 3350 2850 3350 + 4300 3900 4100 3900 Wire Wire Line - 2850 3350 2850 3300 + 4100 3900 4100 3850 Wire Wire Line - 3050 3350 3250 3350 + 4300 3900 4500 3900 Wire Wire Line - 3250 3350 3250 3300 -Connection ~ 3050 3350 + 4500 3900 4500 3850 +Connection ~ 4300 3900 $Comp -L Simulation_SPICE:VDC V +L Simulation_SPICE:VDC V1 U 1 1 61F2BB51 -P 4750 1000 -F 0 "V" H 4880 1046 50 0001 L CNN -F 1 "5V" H 4620 1000 50 0000 R CNN -F 2 "" H 4750 1000 50 0001 C CNN -F 3 "~" H 4750 1000 50 0001 C CNN -F 4 "Y" H 4750 1000 50 0001 L CNN "Spice_Netlist_Enabled" -F 5 "V" H 4750 1000 50 0001 L CNN "Spice_Primitive" -F 6 "dc(1)" H 4880 909 50 0001 L CNN "Spice_Model" - 1 4750 1000 +P 6000 1550 +F 0 "V1" H 6130 1596 50 0001 L CNN +F 1 "5V" H 5870 1550 50 0000 R CNN +F 2 "" H 6000 1550 50 0001 C CNN +F 3 "~" H 6000 1550 50 0001 C CNN +F 4 "Y" H 6000 1550 50 0001 L CNN "Spice_Netlist_Enabled" +F 5 "V" H 6000 1550 50 0001 L CNN "Spice_Primitive" +F 6 "dc(1)" H 6130 1459 50 0001 L CNN "Spice_Model" + 1 6000 1550 -1 0 0 1 $EndComp $Comp -L power:GNDREF GND? +L power:GNDREF GND U 1 1 61F5552B -P 5300 5750 -F 0 "GND?" H 5388 5713 50 0000 L CNN -F 1 "GNDREF" H 5305 5577 50 0001 C CNN -F 2 "" H 5300 5750 50 0001 C CNN -F 3 "" H 5300 5750 50 0001 C CNN - 1 5300 5750 +P 6550 6300 +F 0 "GND" H 6638 6263 50 0000 L CNN +F 1 "GNDREF" H 6555 6127 50 0001 C CNN +F 2 "" H 6550 6300 50 0001 C CNN +F 3 "" H 6550 6300 50 0001 C CNN + 1 6550 6300 1 0 0 -1 $EndComp Wire Wire Line - 4200 2450 4400 2450 + 5450 3000 5650 3000 Wire Wire Line - 4750 1900 5300 1900 + 6000 2450 6550 2450 Wire Wire Line - 5300 1900 5300 2150 + 6550 2450 6550 2700 Wire Wire Line - 3900 2450 3650 2450 + 5150 3000 4900 3000 $Comp -L Switch:SW_Push RSTB +L Switch:SW_Push RSTB1 U 1 1 61FC0519 -P 3900 1950 -F 0 "RSTB" H 3900 2143 50 0000 C CNN -F 1 "SW_Push" H 3900 2144 50 0001 C CNN -F 2 "" H 3900 2150 50 0001 C CNN -F 3 "~" H 3900 2150 50 0001 C CNN - 1 3900 1950 +P 5150 2500 +F 0 "RSTB1" H 5150 2693 50 0000 C CNN +F 1 "SW_Push" H 5150 2694 50 0001 C CNN +F 2 "" H 5150 2700 50 0001 C CNN +F 3 "~" H 5150 2700 50 0001 C CNN + 1 5150 2500 1 0 0 -1 $EndComp Wire Wire Line - 3650 1950 3700 1950 + 4900 2500 4950 2500 Wire Wire Line - 3650 1950 3650 2100 + 4900 2500 4900 2650 Wire Wire Line - 4200 1600 4200 1950 + 5450 2150 5450 2500 Wire Wire Line - 4200 1950 4100 1950 + 5450 2500 5350 2500 Wire Wire Line - 3650 2100 4600 2100 + 4900 2650 5850 2650 Wire Wire Line - 4600 2100 4600 1900 + 5850 2650 5850 2450 Wire Wire Line - 4600 1900 4750 1900 -Connection ~ 3650 2100 + 5850 2450 6000 2450 +Connection ~ 4900 2650 Wire Wire Line - 3650 2100 3650 2450 -Connection ~ 4750 1900 + 4900 2650 4900 3000 +Connection ~ 6000 2450 Wire Wire Line - 5400 1900 5300 1900 -Connection ~ 5300 1900 + 6650 2450 6550 2450 +Connection ~ 6550 2450 $Comp L Sensor:BME280 BME280 U 1 1 61E953D6 -P 7350 2700 -F 0 "BME280" H 6921 2746 50 0000 R CNN -F 1 "BME280" H 6921 2655 50 0001 R CNN -F 2 "Package_LGA:Bosch_LGA-8_2.5x2.5mm_P0.65mm_ClockwisePinNumbering" H 8850 2250 50 0001 C CNN -F 3 "https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME280-DS002.pdf" H 7350 2500 50 0001 C CNN - 1 7350 2700 +P 8750 2600 +F 0 "BME280" H 8321 2646 50 0000 R CNN +F 1 "BME280" H 8321 2555 50 0001 R CNN +F 2 "Package_LGA:Bosch_LGA-8_2.5x2.5mm_P0.65mm_ClockwisePinNumbering" H 10250 2150 50 0001 C CNN +F 3 "https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME280-DS002.pdf" H 8750 2400 50 0001 C CNN + 1 8750 2600 -1 0 0 -1 $EndComp Wire Wire Line - 6750 2600 6550 2600 -Wire Wire Line - 6550 2600 6550 4350 -Wire Wire Line - 5400 2150 5400 1900 + 6650 2700 6650 2450 $Comp L Display_Character:NHD-0420H1Z LCD U 1 1 61E937A6 -P 3150 4650 -F 0 "LCD" V 3150 3806 50 0000 R CNN -F 1 "NHD-0420H1Z" H 3150 3670 50 0001 C CNN -F 2 "Display:NHD-0420H1Z" H 3150 3750 50 0001 C CNN -F 3 "http://www.newhavendisplay.com/specs/NHD-0420H1Z-FSW-GBW-33V3.pdf" H 3250 4550 50 0001 C CNN - 1 3150 4650 +P 4400 5200 +F 0 "LCD" V 4400 4356 50 0000 R CNN +F 1 "NHD-0420H1Z" H 4400 4220 50 0001 C CNN +F 2 "Display:NHD-0420H1Z" H 4400 4300 50 0001 C CNN +F 3 "http://www.newhavendisplay.com/specs/NHD-0420H1Z-FSW-GBW-33V3.pdf" H 4500 5100 50 0001 C CNN + 1 4400 5200 0 1 1 0 $EndComp Wire Wire Line - 2850 2750 4400 2750 + 4100 3300 5650 3300 Wire Wire Line - 3250 2950 4400 2950 + 4500 3500 5650 3500 Wire Wire Line - 3750 4250 3750 3150 + 5000 4800 5000 3700 Wire Wire Line - 3750 3150 4400 3150 + 5000 3700 5650 3700 Wire Wire Line - 3650 4250 3650 3250 + 4900 4800 4900 3800 Wire Wire Line - 3650 3250 4400 3250 + 4900 3800 5650 3800 Wire Wire Line - 3550 4250 3550 3350 + 4800 4800 4800 3900 Wire Wire Line - 3550 3350 4400 3350 + 4800 3900 5650 3900 Wire Wire Line - 4400 3450 2850 3450 + 5650 4000 4100 4000 Wire Wire Line - 2850 3450 2850 4250 + 4100 4000 4100 4800 Wire Wire Line - 4400 3550 2750 3550 + 5650 4100 4000 4100 Wire Wire Line - 2750 3550 2750 4250 + 4000 4100 4000 4800 Wire Wire Line - 4400 3650 2650 3650 + 5650 4200 3900 4200 Wire Wire Line - 2650 3650 2650 4250 + 3900 4200 3900 4800 Wire Wire Line - 4400 3750 2550 3750 + 5650 4300 3800 4300 Wire Wire Line - 2550 3750 2550 4250 + 3800 4300 3800 4800 Wire Wire Line - 2350 4650 2100 4650 + 3600 5200 3350 5200 Wire Wire Line - 2850 5050 2850 5250 + 4100 5600 4100 5800 Wire Wire Line - 4750 1400 1950 1400 + 6000 1950 3200 1950 Wire Wire Line - 1950 1400 1950 5700 + 3200 1950 3200 6250 Wire Wire Line - 1950 5700 2850 5700 + 3200 6250 4100 6250 Wire Wire Line - 2850 5700 2850 5550 -Connection ~ 4750 1400 + 4100 6250 4100 6100 +Connection ~ 6000 1950 Wire Wire Line - 4750 1400 4750 1900 + 6000 1950 6000 2450 Wire Wire Line - 5300 5050 5300 5150 -Connection ~ 5300 5150 + 6550 5600 6550 5700 +Connection ~ 6550 5700 Wire Wire Line - 3950 4650 4150 4650 + 5200 5200 5400 5200 Wire Wire Line - 4150 4650 4150 5700 -Connection ~ 2850 5700 + 5400 5200 5400 6250 +Connection ~ 4100 6250 Wire Wire Line - 2950 5050 2950 5150 + 4200 5600 4200 5700 Wire Wire Line - 2950 5150 3800 5150 + 4200 5700 5050 5700 Wire Wire Line - 2850 5700 3800 5700 + 4100 6250 5050 6250 $Comp -L Device:R_POT_US POT +L Device:R_POT_US POT1 U 1 1 6208C3F4 -P 3800 5400 -F 0 "POT" H 3733 5446 50 0000 R CNN -F 1 "10kΩ" H 3733 5355 50 0000 R CNN -F 2 "" H 3800 5400 50 0001 C CNN -F 3 "~" H 3800 5400 50 0001 C CNN - 1 3800 5400 +P 5050 5950 +F 0 "POT1" H 4983 5996 50 0000 R CNN +F 1 "10kΩ" H 4983 5905 50 0000 R CNN +F 2 "" H 5050 5950 50 0001 C CNN +F 3 "~" H 5050 5950 50 0001 C CNN + 1 5050 5950 1 0 0 -1 $EndComp Wire Wire Line - 3950 5400 4050 5400 + 5200 5950 5300 5950 +Wire Wire Line + 5300 5950 5300 5300 +Wire Wire Line + 5300 5300 5200 5300 +Wire Wire Line + 5050 5800 5050 5700 +Connection ~ 5050 5700 +Wire Wire Line + 5050 5700 6550 5700 +Wire Wire Line + 5050 6100 5050 6250 +Connection ~ 5050 6250 Wire Wire Line - 4050 5400 4050 4750 + 5050 6250 5400 6250 Wire Wire Line - 4050 4750 3950 4750 + 3350 6150 6550 6150 Wire Wire Line - 3800 5250 3800 5150 -Connection ~ 3800 5150 + 3350 5200 3350 6150 +Connection ~ 6550 6150 Wire Wire Line - 3800 5150 5300 5150 + 6550 6150 6550 6300 Wire Wire Line - 3800 5550 3800 5700 -Connection ~ 3800 5700 + 6000 1750 6000 1950 Wire Wire Line - 3800 5700 4150 5700 + 6550 5700 6550 6150 Wire Wire Line - 2100 5600 5300 5600 + 6650 5600 6650 5700 Wire Wire Line - 2100 4650 2100 5600 -Connection ~ 5300 5600 + 6650 5700 6550 5700 Wire Wire Line - 5300 5600 5300 5750 + 8650 6150 6550 6150 Wire Wire Line - 6550 4350 6200 4350 + 8650 1950 6000 1950 Wire Wire Line - 6200 4450 6650 4450 + 7450 4900 7550 4900 Wire Wire Line - 6650 4450 6650 2800 + 7550 4900 7550 2500 Wire Wire Line - 6650 2800 6750 2800 + 7550 2500 8150 2500 Wire Wire Line - 6750 2400 6450 2400 + 7450 5000 7650 5000 Wire Wire Line - 6450 2400 6450 4550 + 7650 5000 7650 2700 Wire Wire Line - 6450 4550 6200 4550 + 7650 2700 8150 2700 Wire Wire Line - 4750 1200 4750 1400 + 7450 5100 7750 5100 Wire Wire Line - 5300 5150 5300 5600 + 7750 5100 7750 2300 Wire Wire Line - 5400 5050 5400 5150 + 7750 2300 8150 2300 Wire Wire Line - 5400 5150 5300 5150 + 8650 3200 8650 3700 +Connection ~ 8650 3700 +Wire Wire Line + 8450 3700 8650 3700 $Comp -L Switch:SW_Push BTN -U 1 1 620E2C06 -P 8550 4650 -F 0 "BTN" H 8550 4843 50 0000 C CNN -F 1 "SW_Push" H 8550 4844 50 0001 C CNN -F 2 "" H 8550 4850 50 0001 C CNN -F 3 "~" H 8550 4850 50 0001 C CNN - 1 8550 4650 - 1 0 0 -1 +L Device:LED LED1 +U 1 1 61E8F822 +P 8300 3700 +F 0 "LED1" H 8293 3825 50 0000 C CNN +F 1 "LED" H 8293 3826 50 0001 C CNN +F 2 "" H 8300 3700 50 0001 C CNN +F 3 "~" H 8300 3700 50 0001 C CNN + 1 8300 3700 + -1 0 0 1 $EndComp Wire Wire Line - 7250 3300 7250 5600 + 8100 3700 8150 3700 Wire Wire Line - 7250 5600 5300 5600 + 7450 3700 7800 3700 +$Comp +L Device:R_US R3 +U 1 1 61F053CB +P 7950 3700 +F 0 "R3" H 8018 3746 50 0000 L CNN +F 1 "330Ω" H 8018 3655 50 0000 L CNN +F 2 "" V 7990 3690 50 0001 C CNN +F 3 "~" H 7950 3700 50 0001 C CNN + 1 7950 3700 + 0 -1 -1 0 +$EndComp Wire Wire Line - 7250 2100 7250 1400 + 8650 2000 8650 1950 Wire Wire Line - 7250 1400 4750 1400 + 8650 3700 8650 6150 +$Comp +L Switch:SW_Push BTN1 +U 1 1 620E2C06 +P 9800 5200 +F 0 "BTN1" H 9800 5393 50 0000 C CNN +F 1 "SW_Push" H 9800 5394 50 0001 C CNN +F 2 "" H 9800 5400 50 0001 C CNN +F 3 "~" H 9800 5400 50 0001 C CNN + 1 9800 5200 + 1 0 0 -1 +$EndComp $EndSCHEMATC diff --git a/c_embedded/src/main.c b/c_embedded/src/main.c @@ -1,33 +1,134 @@ #define NO_BIT_DEFINES #include <pic14regs.h> #include <stdint.h> +#include <string.h> static __code uint16_t __at (_CONFIG) __configword = - _FOSC_HS & _WDTE_OFF & _LVP_OFF & _WRT_OFF & _BOREN_OFF; - -#define LED_PORT PORTBbits.RB0 -#define LED_TRIS TRISBbits.TRISB0 + _FOSC_HS & _WDTE_OFF & _PWRTE_ON & _LVP_OFF & _WRT_OFF & _BOREN_ON & + _CPD_OFF & _CP_OFF; #define _XTAL_FREQ 16000000 +#define TIMER_INTERNAL_CLK (0 << 5) +#define TIMER_RISING_EDGE (0 << 4) +#define TIMER_PRESCALER_TMR0 (0 << 3) +/* + * The last 3 bits of OPTION_REG indicate the prescalar value. In this case, + * 111 indicates a prescalar value of 256. + */ +#define TIMER_PRESCALER_256 0b111 +/* + * We want 1ms delay @ 16Mhz with 256 prescalar, the formula is: + * + * 256 - ((delay * (fosc / 1000)) / (prescalar * 4)) = + * 256 - ((1 * (16000000 / 1000)) / (256 * 4)) = + * 256 - ((1 * 16000) / (256 * 4)) = + * 256 - 15.625 = ~240 + * + * sdcc, there's no overflow here... + */ +#define TIMER_DELAY (256 - ((1 * (_XTAL_FREQ / 1000)) / (256 * 4))) +#define TIMER_REQ_MAX 3 + +#define LED1_PORT PORTBbits.RB0 +#define LED1_TRIS TRISBbits.TRISB0 + +#define OUTPUT 0 +#define INPUT 1 + +typedef void (*ev_handler)(void); + +struct timer_req { + ev_handler handler; + uint32_t rate; + uint32_t cnt; +}; + +static void tmr0_init(void); +static int tmr0_set_event(ev_handler, uint32_t); +static void tmr0_isr(void) __interrupt; +static void led_blink(void); + +static struct timer_req reqs[TIMER_REQ_MAX]; + +void +tmr0_init(void) +{ + struct timer_req *r; + uint8_t i; + + for (i = 0; i < TIMER_REQ_MAX; i++) { + r = &reqs[i]; + r->handler = NULL; + r->rate = 0; + r->cnt = 0; + } + OPTION_REG = 0; + OPTION_REG |= TIMER_INTERNAL_CLK | TIMER_RISING_EDGE | + TIMER_PRESCALER_TMR0 | TIMER_PRESCALER_256; + TMR0 = TIMER_DELAY; + /* + * Enable, TMR0 Interrupt Enable bit, Global Interrupt Enable bit + * and Peripheral Interrupt Enable bit. + */ + INTCONbits.TMR0IE = 1; + INTCONbits.GIE = 1; + INTCONbits.PEIE = 1; +} + +int +tmr0_set_event(ev_handler handler, uint32_t rate) +{ + struct timer_req *r; + uint8_t i; + + for (i = 0; i < TIMER_REQ_MAX; i++) { + r = &reqs[i]; + if (r->handler != NULL) + continue; + r->handler = handler; + r->rate = rate; + r->cnt = 0; + return (1); + } + return (0); +} void -delay(uint16_t iter) +tmr0_isr(void) __interrupt { - uint16_t i; + struct timer_req *r; + uint8_t i; - for (i = 0; i < iter; i++) - __asm nop __endasm; + if (INTCONbits.TMR0IF != 1) + return; + for (i = 0; i < TIMER_REQ_MAX; i++) { + r = &reqs[i]; + if (r->handler == NULL) + continue; + if (++r->cnt == r->rate) { + r->handler(); + r->cnt = 0; + } + } + TMR0 = TIMER_DELAY; + INTCONbits.TMR0IF = 0; } -void main(void) +void +led_blink(void) { - LED_TRIS = 0; /* Pin as output */ - LED_PORT = 0; /* LED off */ - - while (1) { - LED_PORT = 1; /* LED on */ - delay(30000); /* ~500ms @ 4MHz */ - LED_PORT = 0; /* LED off */ - delay(30000); + LED1_PORT ^= 1; +} + +void +main(void) +{ + tmr0_init(); + tmr0_set_event(&led_blink, 1000); /* blink every 1 sec */ + + LED1_TRIS = OUTPUT; + LED1_PORT = 0; /* off */ + + for (;;) { } }