How to use the Atmel ATTINY10 Microcontroller

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Glad Ben Heck is still making videos!

👍︎︎ 1 👤︎︎ u/whatthehellisplace 📅︎︎ Jan 17 2019 🗫︎ replies
Captions
this is a dime this is a grain of rice and this is the eighty tiny ten microcontroller look how small it is this microcontroller might be small but it can do quite a few things so we're gonna talk about today is how to get started with this microcontroller wiring up a programming jig then I'm gonna show you how to use the software to actually program it and use the 12 volt high voltage programming mode so the reset line can be used as i/o and then we'll make a sample program where this thing actually does something somewhat useful alright let's get started okay here's the data sheet for the Atmel 80 tiny 10 microcontroller let's go to the pin configuration right away alright so we have this package SOT 23 so there's only four i/o pins on it and one of them is the reset line so what we want to do is we want to use that reset line as an i/o in order to do that we need to have a high voltage programmer so there's a three common programmers for Atmel devices there's the AVR isp mark - there's the AVR dragon and the app male ice now each one of these can handle different devices and has different features but there are a few gotchas for instance this micro uses the TPI interface the tiny programming interface and none of the programmers I just mentioned have the ability to do high voltage programming and the TPI mode even though this chip supports high voltage programming okay so basically we put the reset pin low and then we can put data on the TPI clock and data lines and program the device or if we put 12 volts on the reset pin we can also program the device so basically zero volts or 12 volts on the reset line will put this device into programming mode so what we need to do is make an adapter so the standard AVR isp mark 2 can do high voltage programming here's another drawing we just have our 80 tiny 10 here this is the standard pin configuration of the AVR isp mark to master and slave out clock master out slave in voltage reset ground this is what you'd use for most of your standard AVR devices like an Arduino so if the devices in TPI mode which it does support it will be this pin out data clock so this is going to be half duplex data resets the same this one's not used and then you have your target voltage and ground so we need to do is we need to take this into Eagle and drop a schematic okay now we're in Eagle this is all you need to make a high voltage programming converter for the AVR is be mark 2 okay so we have a header block here this is going to be 12 volts 5 volts and ground we'll get that from just your standard PC power supply the ISP header for programming it's labeled for spy mode but we'll just wire it up for TPI mode then we have our 80 tiny representation right here going to have a few resistors an indicator LED a PNP transistor I'm using a tip 107 an opto coupler and then three toggle switches so the programmer has its reset line right here and it's active low which means it's normally high so we can actually use that to drive the opto coupler so if you don't know what NAPA coupler does basically it's a little package that has an LED inside of it and also a photo transistor that reacts to the LED so the LED is illuminated by passing current through it the photo transistor will saturate allowing current through it so basically this allows you to control something else but have complete electrical isolation from it so yeah check this out I'm gonna take this resistor and I'm duplicated kaboom and let's go over here let's take this let's add some lines 12v 5v and ground cool then up here let's connect this resistor to the opto coupler because it's still an LED so you still need to have a current limiting resistor on it now we can just go to our label right here and call this five volts so we're gonna connect five volts through a resistor and let's just use you know one hundred ohms is pretty common value all right so the reason we're doing it this way is because as we mentioned reset is active low so we have five volts going into the LED here but if we have five volts here the LED won't illuminate it'll only illuminate if there's a path to ground so what we're gonna do is we're gonna come down here and go to the reset line so if reset goes low then current will pass through the LED boom the opto coupler is activated okay so this is our PNP transistor we have the emitter base collector so here's how we're going to do this we're going to attach the base to the collector of the photo transistor on the opto coupler right okay so this resistor we're gonna have looking to make make a 10k so let's attach 12 volts to the emitter of the PNP transistor and have the 10k resistor pull the base up to 12 volts as well so in a PNP when the base is high the PNP will not allow current through when the base goes low that's when current can go through so I point it high we basically ensure that it is stable until we want it to conduct so when we do here the other side of the transistor and as you can see this is an NPN transistor this one will go to ground all right so when nothing's going on when this reset line is high this LED cannot light and reset goes low current flows through the led thus activating the photo transistor now what happens here is this is pulled high but what's a photo transistor is activated this will be pulled low to ground and boom the 12 volts passes through the NPN transistor and we have 12 volts on the collector so let's call that program 12 because it's different than the 12-volt power supply source the reason you have to hook up voltage to the VCC line is because if the programmer does not see the correct operating range it will not program all right let's bring our microcontroller down here and we'll connect ground and power to this as well Wow Eagles are really noisy program bling-bling-bling all right so this is where the toggle switch has come in so here's the deal we have our data line in our clock line for programming and the 80 tiny but if we're bread boarding we're probably also going to be hooking other things up to it ladies switches sensors etc and if those things are hooked up while you're trying to program it it might prevent it from going into programming mode properly because they'd be a load on the line that would affect the programmer not always but often that's why I have these toggle switches so the toggle switch has three poles on it the center pole is what's connected to the shaft and that either goes to one pole or the other so it basically allows you to use it as a selector that is the TPI data line which lines up to the upper-left pin on the programmer okay let's go back over here so let's do this let's go TPI data goes here so position one is going to go to the data line on the programmer same thing the clock line and then this will go down to the clock line of the record controller and the other side of the toggle switch that will simply go to a row of headers that we can use for programming same thing with the reset line let's go to the toggle switch and that's either going to go to program 12 volts or again a bank of headers that's especially important in this case because we don't want to put 12 volts into you know a Hall effect sensor or you know an LED oh there's one more thing that we need we need to take a resistor here and connect it to the 12 volt program line and have it go to ground alright and we'll make that a 1k cool okay let's go over the circuit one final time we have our input terminal block we'll hook that up to a PC power supply 5 volts goes into a 100 ohm resistor it tries to illuminate this LED but I can't as long as the programmer reset line is high oh that reminds me we do need one more thing let's grab this resistor once again break that and let's put a pull-up on this 10 K is a good value okay this is gonna look a little sloppy there we go ok now we have the whole circuit here let's go over it we have a terminal block which is going to bring 12 volts 5 volts and ground from a PC power supply it's a very useful thing to get power from 5 volts goes into a 100 ohm resistor and it tries to illuminate the optic couplers LED but can only work if the reset line goes low and then we have a pull-up from that to 5 volts again to ensure that it doesn't illuminate unless we actually want it to unless the programmer wants it to other side of the upper coupler we have a photo transistor and when it's saturated it will pull the base of a PNP transistor low allowing 12 volts to flow into the program 12 volt line which has a 1k pull down to ground to keep it stable when it's not activated that goes into a toggle switch and if the toggle switch see how all the toggle switches are the one position means program so if the toggle switches all in the same direction that means the microcontroller is hooked up to the programmer in programming mode if you break all of the switches the microcontroller is hooked up to a standard header Bank for prototyping purposes microcontroller also has ground 5 volts and indicator LED in it if you only have 4 i/o and you lose one of them to a reset line getting that IO back is actually pretty important ok now that we've discussed the schematic let's wire it up in a breadboard form and see if it works ok so I have this bin of surface mount adapters here I get these from Marlin P Jones & Associates let's see oh oh right there right there top 23 the little surface mount adapter on which we can mount our chips so we're gonna go three and three with some headers and the reason I'm using this board is so that the headers will be aligned straight for when we put them into email receptacles later on now a lot of people are scared of surface mount soldering they're like oh it's impossible alright so this is one two three so I'm gonna tin the third pad down on the right there's gonna be a mark on the chip which is pin one I'm gonna do is going to make sure that the chip is flat it's not passed into the tweezers then really the main trick is positioning it correctly at the start so I'm going to position it and then just hit that one lower right hand pin so it stays in position right flip around upper right hand pin a lot of people might think oh I can't solder that accurately well that's the thing with surface mine you don't have to solder accurately watch this boy oh no so you can just as long as it's in the right place you can just blob solder on and who cares all you have to do is clean it up afterwards so to do that couple ways you can do it you just make sure your iron is nice and clean then you just kind of grab the edge of it and pull it away basically you're just giving the solder a lot of surface area on the iron to be attracted to and that pulls it away from the pins of course the other way you can do it is with desoldering braid this stuff it's basically like a woven copper being a woven copper it has increased surface area for sucking up solder so with that you just kind of put the braid on the soldered press down and it'll suck it right up you do want to be a little careful sometimes you can suck up too much solder and have a cold cold joint left over I don't have any three by one's just for buy ones but that's fine so again I'm going to put the headers together I've got some of this Radio Shack be quite prototyping board when RadioShack was going out of business and had their online sales Felix and I went online we bought as much of this bakelite as we could I think I bought like Oh God $1,500 worth of it done closeout so I have a lot if you look says a lot - we both bought tons of it I don't know I like it because it's well it's easy to break because it's bakelite not fr4 and only has pads on one side which might seem like a limitation but actually when you have plated through-hole via boards that actually can be tricky because you have to think about things a little bit differently when you're soldering them okay here's the board all wired up we have our 12 volt 5 volt ground terminal block inputs here tip 107 PNP transistor our programming header that goes the isp mark - optocoupler for the 12 volt control indicator LED a couple of the resistors we talked about here's a toggle switches someone there to the right that means we're in programming mode to the left is in run mode then we have headers on either side labeled P V 0 PB 1 PB 2 PB 3 PB 3 is also the reset line we don't have a toggle switch on PB 2 because it's not used for anything else then I also have a voltage and ground header banks here and then also some buttons that we can hook up if we want to just take a quick look at the back I basically did it with a bunch of cutoff leads from all of my resistance and whatnot I keep all the leads well most of them for this purpose and yeah basically just did everything with single-sided RadioShack bakelite board ran a bunch of power traces ground traces and then jumped as little there's also a few surface mount resistors in here like right there but if you can see it that's the 10k pull-up for the chip 107 so this might look like a lot but the thing is once you've programmed the chip then this or just the chip is all you really need so you develop on this and then you embed it in something else okay we're gonna be using a small PC power supply for this when I use terminals like this I like to double up the wires just to make them a little bit thicker and insure a good connection so this thing will turn on when the PC power supply is turned on and to do that you basically find the green wire on the power plug and then you connect it to ground in my case I've just added a toggle switch to do it and that will activate your PC power supply pretty simple okay so I'm just gonna take my AVR isp mark 2 and plug it in here and we should be good to go let's plug this led into PB 2 for our first test okay we're going to program this thing using Atmel studio 7 this is the official IDE integrated development environment for Atmel AVR devices okey so the first thing we want to do is go up to tools device programming okay there's our tool there's a device interface TP I hit apply and then see if we can read the signature off of the chip great we got a signature and a target voltage which is bang-on of course that might vary slightly see that alright let's go down to fuses okay there's only a few on this device clock out which puts the internal clock onto an external pin watchdog timer and then the one that were really curious about reset disabled so if we enable this will no longer be able to use a logic level low to reset the chip we'll have to use 12 volts it's actually gonna warn us about that are you sure yes I'm sure okay let's go to new project Paul C C C++ executable great let's call it Tiny Tim in the spirit of the holidays all right so eighty tiny tiny okay all right here's our project Tiny Tim so since you're you know probably going to use i/o it just puts that library there for you there automatically there's other libraries we'll probably want to add later on so hmm unlike Arduino which you might be more familiar with in C you have to have a main loop and then your infinite loop has to be manually set up inside of that so if we didn't have a while loop here if we just you know put a bunch of code here straight it would execute and then the program would end actually so we have to have a while loop there all right so if we look at the data sheet for this device let's go into the pins okay so PB 2 doesn't have a toggle switch on that's that's the easiest one to use if I oh poor it's where's the magic there it is so here's our registers so we're not using libraries we're using registers D D which is Direction port turns of pins on or off if they're outputs pull up an able adds a pull up resistor internally if they're inputs and pin allows you to read the values if they're inputs so let's go back over here and we can do our one-time setup before the while so DDR B and C since this is based off Visual Studio it'll autocomplete things for us that's nice so it's easy to think of this in binary so 0 B for binary 1 2 3 4 5 6 7 to 8 so even though there's only 4 pins on this device this register is still an 8-bit register so it's PB 2 so that's the third pins we go 1 2 3 set that one to high and that's going to be the output pin okay so now on the main loop let's just really simply port B equals you can do this or what most people do is you have one denta two so you have the first bit and then you bit shift it to to the left which puts it in the PB to position cool so port B equals that and then if you want to inverse it will actually raced you in a straight assignment so then port B equals that now port B equals zero cool let's see if that works let's program it okay there we go the LED is on but why is it stuck on even though we are tied with it on and off well that's because it's toggling faster than we can see okay so when we're getting into timing we're gonna want to basically know what the speed of the chip is so first we need the configuration change protection register CCP and basically what you do is you put a value into this register and that allows you to change other protected registers you have two cycles with which to do it so we're gonna go see CP equals d8 and hex that's the code unlock protected registers now that we've done that we have four cycles to change things clock pre scalar which other does clock prescaler register all right so this register is the division factor so one two four eight so that's the default so eight megahertz divided by eight means it's one megahertz so if we want to get the full speed out of this thing we need to change this register you can divide it quite a bit actually so but we want it to be zero so we're gonna go back over here and after we've opened up the register we're gonna put a zero in here no divider equals eight megahertz cool now if you want to actually use that delay we need to go up here and man I can't type today include AVR see how it finds it for us Oh Thank You intellisense okay AVR delay but even before we do that we need one more thing and we need to specify the frequency of the CPU so we're setting the you know the CPU frequency in our in our registers but we also have to tell you know our compiler what it is so it's eight one two three one two three eight million all right cool so now that it has this this information and this it can now create a proper delay so we come down here okay so we go down here let's see what we have for delay microseconds and milliseconds all right we want milliseconds cool let's try it there we go it's a thing of beauty hey let's do this blinking a little bit more elegantly so let's go down here it's called void I don't know pins and then well we only have one port so we're gonna have let's see it's called au int eight the pin you int eight the state now notice how I'm using u into eight which is an unsigned integer eight bits I'm doing that as it is slapping an int because this thing only has 32 bytes of RAM so we want to make sure we use the smallest possible variable types to hold the data we need if the state so if that's a 1 we execute this else we execute this right so if it's a 1 port B or equals that way it won't affect any of the other bits one bit shifted the pin to the left C now if the state is a 0 you go port B and equals the inverse of 1 bit shift the pin to the left so we'll go pins so it's - because its PB - and then it's 1 right pin 2 is a 0 let's see if it works uh-oh we got an error because we called a function before it was declared so there's two ways we can fix this we can copy this and put it before the main loop like that which should compile yep oh the other thing you can do is you can create a function prototype so you can go down here basically you just take this and then you bring it up here this is like what you might see in a header files you just do that and then it will also work so I mean this function is you know kind of luxurious for such a small memory footprint however if you're you know doing a lot of bit wise operations it might work well although if you're in a really time-sensitive you know loop of some kind if you jump out to this this means you have a jump instruction than another jump instruction here to execute the if-then and jump instructions actually consume a lot of cycles I think it's two cycles no matter what is consumed by a jump yeah so it's handy but it's slower we should check to see if our reset line is truly usable so I'm gonna move the led over to it okay let's just change this to a three send it over and oh you know why it's not working because our DDR bu has not changed so let's let's just make a you know one two three four let's make them all outputs reprogramming flip it back and you see how it's blinking but it's dimmer so that shows you that this pin can be used as i/o but it's not going to have as much source or sink capacity compared to the other three let's do an input example next so I'm going to move the LED over here to PB 0 just so it's a little bit more out of the way I've got these buttons here with headers on them so I can easily hook them up into my circuit let's look up this button to PB 2 and ground now for something completely different alright so we're using PB 2 as an input so we're going to set the third bit here to as 0 all right so that's going to be an input and then we're also going to use the pull up an able register so let's just double-check that pull up an able or pew for short okay so we're gonna want a zero on the DDF B register and a 1 on the pull up unable register so yeah do pull up enable B equals basically an inverse of that C so instead of having to add a pull-up resistor on our circuit there's one built in to the microcontroller we just have to enable it Wow we saved one sent okay so how do we detect if that buttons been pushed well we need to do more binary bit masking if pin B and so let's just type it something binary okay 1 2 3 4 0 1 1 2 so you know basically the same value so we're going to take the input register an and it with this and since there's only one bit set to 1 that means if there's anything there this will return a true remember it's it's digital logic so like if you put a 0 here and you add the you know PB with 0 that means it wouldn't have anything so if this bit is high that means it's a default state then something then it will do one thing else if that bit is low if the button is pushed it'll do something else so what we can do is we can actually just sit haven't the if-then-else we can just invert this logic here so if it's not high so if you take this port and you end it with this number and the result is no bits of Romanian that means the buttons been pushed that means something will happen so what we can do here is you say ok so if the button is pressed let's do pins 0 1 which is where our LED is now else so I guess we did need an else after all pins 0 0 ok let's try it out send it over ok let's turn that over so it's enabled there you go pretty simple so now that it's hooked up to the oscilloscope let's do a quick test pins 0 1 pins 0 0 alright so we run this and we get a frequency of 1 point 9 3 approximately you might be thinking that's not 8 megahertz but remember we have loops we have branching we have an if-then all that takes time although it's still pretty efficient because think about it this is just the frequency of the peaks the valleys are also an instruction as well so it's really running twice this speed so it's closer to four megahertz which is actually really good considering it's just an 8 megahertz processor so the C compiler is doing a really good job of making this as efficient as possible like if you wanted to make it completely cycle accurate you could do assembly but this actually isn't too far off from assembly so you know this chip is better for doing serial things like this then it is obviously i/o input because this hardly has any so what what sort of cool serial stuff could we do with this chip well what about a UART like a 9600 baud serial device because then this thing could send information to other devices if you think about serial communication 9600 baud well that's 9,600 bits per second right and if you're talking serial there's also a start bit before the eight bits of data so that actually is counted in it so even if it's 90 senator bits per second it's not necessarily 9600 divided by eight bytes per second because each data packet has nine bits so we need to take the into account so basically what we need to do is we need to divide the system frequency of eight megahertz by 9600 and we'll do that using timers so there's registers like timer counter control register a and also key and again it's all bitwise these are your registers and this one's kind of weird because one of the registers is actually crossed over both registers see this wgm 0 1 & 1 waveform generation mode bit description right so like what kind of waveform we want to create we have all these choices here and see how it's a 4-bit number so two of the bits are in one register and the other two bits are on the other register isn't that confusing so that's a bit how you doing so let's see what do we want so let's see our clock source is no pre scaling so we want to actually use 8 megahertz clock source so this is going to have a 1 here in the low register so let's actually go over here so we've got TCC our 0 a basically equals nothing 1 2 3 4 5 6 7 8 ok TCC our B this is gonna be a little different so we know the LSB is going to be one because we want no clock to fight or no pre scaling so we wanted to actually count down at eight megahertz so it's going to count you know at full speed then we have these two straggling bits here so we got bit three and bit two of whatever that was okay so this is the one we want clear timer on compare so it's going to count a certain number of cycles then once it reaches that number it's going to clear the timer and then start over again so zero one zero zero okay so the low two bits are zero zero so okay we got that in register a so that's all zeros and then zero one for the other ones so let's go to this so this needs to be zero one so back over here there that should do it so it's going to be no prescaler and then clear timer on compare cool so what's it comparing to well that's another register we need to look at output compare register high byte so if there's a high end low byte basically that's how you have a 16-bit number in an 8-bit system okay so this is going to be we're going to basically use these two registers to create our the number we want to count to so here's a handy way of doing it OCR 0a h4 high by equals something and then OCR 0a low byte equals the lower part of the byte 8 million divided by 96 hundred eight hundred and thirty-three point three three three three three let's go 833 so 833 doesn't fit in a byte a programmer and then we can type eight thirty if you look at in binary it takes more than one byte see so what we do is we've bit shift it so the one one here that's part of the high byte and this pattern here is part of the low byte high bite equals the number bit shift eight to the right 833 bit shifted eight to the right gives us the top half of the byte and for the low half of the byte we take 833 and we end it with one bite okay timer counter interrupts mask register wow this is exciting alright so we are comparing on a so we need to set that bit right there so here's something fun you can do if you actually like copy-paste that from the datasheet and then you can actually type in so Tim's cos 0 or equals and then you do one bit shift that and so this bit pattern is actually a define so the compiler won't know what you mean cool so that will enable an interrupt but the thing is we need to have a place where they don't have to go let's go up here above main void and - is our timer zero compare a vector so when that interrupt triggers which will be 9600 times a second this is our will be called and that'll be done automatically so we don't even need to worry about it in our main loop make sense in fact we could just here let's just leave the main loop empty like that right and then all we're gonna do over here is let's let's just toggle the output pin and we also need to include the AVR interrupt library as well and finally we need to enable interrupts with this command you can also disable interrupts with that command ok so let's see we have our timer set up at 9600 Hertz we're enabling interrupts we have the interrupt library added and then we have an interrupt to execute all right let's send it over and flip the switch hey there we go frequency is not quite right I think we need to tweak that but ya see it's just basically doing a pulse at a certain frequency so what we're actually going to set this up to do is send one bit at a time hey let's talk about the data format for rs-232 style communications so we've got two logic levels high and low 5 volts to 0 volts the old days of rs-232 this was actually a positive 12 negative 12 but this is the modern TTL version okay so you always have a start bit to begin with so that's transition from the 5 volt level to the 0 volt level that tells the system hey here comes a byte right and then the next eight pulses are the bits and we're going to be starting to LSB least significant bits of 0 1 2 3 4 5 6 7 right so if you're ascending a value of just 1 you know like the number 1 this would go high and then the rest of it would be low beautiful drawing right or if you're sending you know 255 the whole thing would be high like that so high is the default state that the system waits in that's why you have to have a low start bit so you have some sort of change which tells the system hey here comes a byte of data right so what we're going to do I think is make like a state machine in the ISR interrupt service routine so it's gonna say ok what bit are we on so based off what bit we're on will either send a 0 or 1 so there'll be nine iterations to this including the final iteration which is after all the bits are sent no matter what go back to high ok let's start by creating some variables static volatile because it's going to be used in an interrupt char one character X MIT that's what we're going to be sending each time static volatile you int eight which bit out so that's how we're going to keep track of our state machine and let's set up a switch case statement switch which bit out so this will be called 9600 times a second no matter what now whether or not there's any data to send that depends on if there's anything in the buffer okay so let's let's do this okay so the first possible thing that could be happening case one because it can't be zero because case zero it means nothing is happening case one is the start bit right so start bit as we mentioned is low turn no port B and equals the inverse of one go low for start bit it's always good to comment your code and now which bit out equals to advance to next state okay so the next thing that can happen is the eight bits of data through new case 2.9 the data the warrant all right so x MIT is going to be the bit we're sending if x MIT x MIT it's kind of a hard word to say if x MIT and one so if the LSB the least significant bit is present in this value then we're going to set that pin high port 1 okay else that's right it's low and equals the inverse of one okay so here's where the magic happens X MIT bit shift one to the right so it's not rotating its bit shifting so it's going to take the 8 bits shift them to the right which means the LSB gets blown to oblivion and then the second bit becomes be so basically we're just putting them all in position so we could send them out one by one and then of course in this case which bit out increment break and of course the final case is 10 which means we're done so in that case port B goes high which is basically the you know now we're done now we're just gonna you know wait for the next thing and which bit out equals zero which means nothing happens now if you notice up here we set which bit out till to tend to start the reason we do that is so when this code executes we'll jump here that line will be made high which again is the you know the default state and then it'll be ready for data so we're basically we did that just to save ourselves a little bit of setup code down the road let's make another function void send byte yeah let's do you into eight the byte X MIT equals the byte so we're passing a byte to this function and then we're copying it into the X bit variable because the X X bit variable is being destroyed as we send it out since it's being bit shifted so this way we have a copy of it okay and then which bit out equals one by sending that to one we basically enable the state machine here to send out the data cool all right so I think that won't work let's go over here let's just do a delay of one second send bite a alright let's try to catch this I see some sort of blip on the scope let's go into trigger and try to catch it there we go and it's all low ASCII a would not look like that so he did something wrong okay I realized one thing we should do is we set which bit out to one and now we should wait for it to be cleared oh that's what I missed let's try to catch this again hey there we go awesome and you see I have my bus on the scope set up to read cereal and there it is hey cool so let's talk about maybe having entire strings so let's add one called no no printout no just think all print you know be like old-school basic right so constant char data okay so the reason we call it a constant charge because we're going to be passing it data from flash that's you know it's not a variable it's it's a constant you know basically hard-coded okay so we have a pointer at data so basically it's a pointer to the string we pass in to this so yeah this is pretty simple wall while there is some data while data does not equal zero zero which is the string terminator you know zero send byte data increment pointer each time boom so now we go down here print hello zoom out catch it BAM yeah there it is hello let's make something that can print numbers because the way a computer thinks of a number is completely different than us decimal you int16 the number right okay so in order to print a you know a binary number in decimal we have to basically figure out the decimal digits so we need a divider for that you int 16 divider equals 10,000 right because 16-bit number goes up to 65535 which means we're never gonna have more than five digits in this number I mean we could have more but you know I think this is plenty for our purposes okay so let's do loop for G equals zero G is less than five so it's 0 1 2 3 4 which is 5 or 5 iterations for 5 digits this is how we do it if the number is greater than or equal to the current divider then we're going to basically divide it and print the digits so we're going to send bite okay so it's the number divided by divider right think about that so you've got let's see let's just say it's 6 5 5 3 5 right divide it by 10,000 gives you 6 right but if we want to turn that into ascii we have to actually add 48 to it to get the ascii value so we send by the number divided by divider plus 48 convert to ascii wow that was fun now we take the number and we do a modulus divide which is divide with remainder divider so that way it will divide it by 10,000 but we'll keep what's left so we can basically chop the number up in bits and bits so so if yeah so basically you would take 6 5 5 3 5 divided by 10,000 which we give you 5 5 3 5 I made some code to try to send the number 200 but you see all we got was 2 yeah we need to figure out what's wrong oh I see the problem if one of the other characters is a 0 then this will never execute so let's put in a flag let's call it 0 why not that's not like I'm going to be a reserved word 0 equals 0 Oh something sometimes you can just define a variable like that but if you want to make sure it's a value should put it in like that so if we find anything at all such as the first two in the or 200 then we'll set 0 to 1 which will now enable us to draw zeros that way we won't see the zeroes in front of the 200 we'll just see it after because otherwise it would print 0 0 to 0 0 right so then we'll do an else if 0 yeah confusing I mean it's all about how you know humans and computers don't think of numbers in the same way there that should do it ok let's try it again let's go for single and tap the button boom there it is 200 let's look this up to a TDI cable that's basically a cable that has a TDI chip built into it so you can easily communicate via serial alright we're just gonna need a ground and receive okay I've got a serial terminal here let's see if we can pick anything up oh there it is 200 debounce as we mentioned is not super great oh we don't have a carriage return I guess it should be your choice if you want to carry at your turn so in ascii it's well it's usually care to turn a new line so we go decimal 200 send byte take a look at this this is what art should be oh yeah alright let's look for this character a new line is 13 10 all right send byte 13 7 byte 10 cool so now we have everything we need to basically get human readable data off of this tiny little micro so let's try one more thing let's hook a sensor up to it so this chip actually has an analog comparator which basically says hey is one pin higher or lower and Folgers and the other the other one is the analog to digital converter that's one we're gonna be using today so what I've done here is I've added a Hall effect sensor so Hall effect sensor is a sensor that detects the press of a magnetic field and outputs an analog voltage relative to the intensity of the field so the Hall effect sensor is hooked up to power and ground and the last thing is this this is the Hall effect sensors output line so we can use the reset line because as we talked about earlier the reset line is a little weak for driving loads but the ADC on it it's not affected at all oh yeah take a look at this so here's the box right so there's 480 sees there's one on each pin however there's actually only one active at a time so basically you're multiplexing at end that's what the MUX means and then you take turns see so we're gonna say okay we want a dc-3 which is the one on the reset line and we want to sample it so it's actually pretty simple so we go to the add MUX and this basically is a register that selects which one you want to use so a dc-3 is the number three in binary pretty simple so we're just gonna go over here add MUX equals three we also need to disable the digital output on that pin so we're gonna go did our zero that's another register equals one two three four that since is the fourth one yeah also of great importance is the ADC control and status register a yeah then we also have to actually enable the ADC so that's a DC sra register and it's take a look here okay so bit seven enables ADC and then the last two bits is the pre scalar so we need to go so it's one one two three one two one one cool enable ADC now to start the conversion we set this bit a DSC start conversion so let's just copy that go down here same register ad CSRA so this is why you know bitwise operations are so important because multiple things are in a single 8-bit register one bit shifted that much to the left start conversion when the conversion is complete that bit will be set to zero by the system so we're gonna wait for that Wow ad CSRA and one dot ad C basically while it's one do nothing wait for conversion okay so when it's done where do we get the value from a DC conversion result now it's 8-bit so we just have the low byte so it's ad CL so we can do over here is we can go decimal ad CL and that will send out the value all right we can see a steady stream of 129 coming off of the ADC so let's bring the magnet in oh I'm gonna flip the magnet around the polarity of the magnet and whether or not it's in front over or behind the sensor actually makes a big difference there we go see that go down low number go up high number go down so yeah we're using the ADC of the eighty tiny 10 to sense the value of the Hall effect sensor and then we're using a bit Bank serial protocol is to send that data to a computer Hey look I was able to fit the first three lines of the Great Gatsby in this thing so now microcontrollers can bore English students to death so there you have it the Atmel eighty tiny 10 to little microcontroller that packs a big punch
Info
Channel: Ben Heck Hacks
Views: 532,450
Rating: undefined out of 5
Keywords: ATTINY10, atmel, microchip, ben heck, RS-232, hall effect sensor, atmel studio 7, shiba inu
Id: ra7bT74EGUs
Channel Id: undefined
Length: 53min 1sec (3181 seconds)
Published: Sun Dec 30 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.