Blinky Blinky
void setup() {
DDRB |= (1 << PB5);
}
void loop() {
PORTB = PORTB | (1 << PB5);
_delay_ms(100);
PORTB &= ~(1 << PB5);
_delay_ms(100);
}
Aodhan145,
I too started with writing a few lines of "direct to MCU" code inside the Arduino IDE - to make PWM using the 16 bit timer so I can get 16 bit PWM. But the rest of the MCU remained unknown to me for a while. I am no expert, so I can understand how the task looks so overwhelming until you actually get started. The trouble really is getting rolling: starting from no platform or tools - how to get a blank MCU to boot and see if it does... After which, things kind of click.
So, beginner to beginner, here is how I got rolling (accidentally in my case):
I started messing around with the 18650 flashlights, I got one, and I hated the way the "driver" works. I managed to do some hardware hacks and get it to work better, but still rather unsatisfactory. I found that there is a driver out there (Nanjg 105c) using the ATTINY13a MCU and some folks out there have been rolling their own software for the ATTINY13A MCU on the Nanjg 105c.
0. Some time ago, I learned how to use the Arduino UNO to flash a virgin 328 chip on the breadboard with the Arduino UNO as the ISP programmer, so I know I have the
tool to burn program onto a blank MCU.
1. So, I got a NINJG1105c driver ($3) a Cree (work alike) LED on aluminum base ($3), an SOIC clip ($4), to be experimented with
using the Arduino as the ISP-programmer.
2. I connected the 105c to the LED, and use an adjustable PSU to simulate the battery. Got the 105c running the LED, and got comfortable with how to control NANJG 105c with a push button (power cycle changes LED brightness - as with a flashlight's on/off clicking switch).
3. I down loaded WinAVR from source forge, and learn how to READ the flash/eeprom/fuse from the chip (ATTINY13a). Now I know I have a hardware platform that
talks from MCU all the way to the PC.
4. I downloaded some publicly available NANJG105c driver (nlite). I have no intention of using it in my light, but I downloaded it onto the MCU and check that the 105c is now running the new firmware properly. Now I know I have bi-directional connection to the ATTINY13a MCU. It can accept programs I will write.
Hardware platform for software development is working.
5. I downloaded a mini-driver (bare bone) source example from a fellow "Dr. Jones." Thanks to Dr. Jones, I know I have working source to
figure out how to use WinAVR to compile.
6. Compiled and downloaded the mini.c and my LED is now controlled by a program I compiled.
7. Now I have
all the tools to write a program, get it down to the MCU, actually see how that works.
8. I downloaded another publicly available more complex driver with source (from a fellow called Johnny C) showing how to "handle the stars". (Stars are connection points if soldered to ground selects different options for the driver. The ATTINY13a does "digitalRead" on them to setup the driver characteristics. Learning to use the Star is to
learn to do IO without the Arduino library.)
9. I use the publicly available source as
reference and examples. I wrote mine from the ground up using those available source (Dr. Jones' mini driver and Jonny C's Star and flashlight-off-timer programs). Thanks to Dr. Jones and Jonny C, they helped me learn.
10. The WinAVR's make creates temporary files. It shows the object code, the data... That is another good source to see what is going on.
11. Now I have the platform to read/write a virgin MCU with just what I wrote. I also made a proto-board with SOIC pin header and DIP switches so my NANO on the protoboard now work as the ISP-programmer, and can be fully disconnected to the MCU (via 6 dip switches) with the SOIC clip still on. I know I can program for any AVR with an ISP connector.
I just finished my "dream" driver (second version)
fully my own code (and no Arduino library), got that into the Nanjg 105c, and the 105c into my flashlight. Not much features, (to show level of complexity), the features are: 2 sets of selectable modes one with 4 different levels of brightness and the other with 6 levels, eeprom-write wear leveling, an auto-off when on full intensity to avoid overheating, and blink out the battery voltage on my command. (3.5v battery will have 3 high intensity blinks followed by 5 low intensity blink) Plus I use a Star (IO-read) to enable/disable to full-intensity timer so, software alone can't disable this safety feature. Oh, I also learned to do the "analogRead" (ADC raw code to read) to time "how long has the light been turned off) by using another Star to ADC read how far a capacity bleed down.
With a working platform, learning can progress easily. The flashlight driver is not an ideal platform but a working platform with everything necessary. Writing a program that "select command by turning the MCU (flashlight) off" is kind of strange and requires some mind twisting. After doing some mind adjustments, it works.
At this point,
my only dependent on the Arduino is using it as an ISP-programmer. I can as easily get any other AVR ISP flasher hardware and thus be completely independent from Arduino.
So now while I cannot claim I am a competent MCU programmer, I think I know the MCU a lot better now than before this project. Now I have turned a bare MCU to a working MCU run a program I developed - freed from some bootloader/library others created. I feel while I don't yet know most of the features of the MCU, I have a platform to experiment. My next project will be replacing the TINY13a with the TINY85V. 1K of flash and 64 bytes of RAM on the 13A is limiting. So I look forward to the arrival of my 85v to do more experiments with.
I hope you find this info helpful.
Rick